【发布时间】:2012-04-05 10:30:51
【问题描述】:
我尝试将 shmget 与 2D 数组一起使用。 这是我的代码:
char **array;
key_t key;
int size;
int shm_id;
int i = 0;
void *addr;
key = // here I get the key with ftok()
size = (21 * sizeof(char *)) + (21 * sizeof(char **));
shm_id = // here I get the shmid with shmget()
if (shm_id == -1) // Creation
{
array = (char **)shmat(shm_id, NULL, SHM_R | SHM_W);
while (i != 20)
{
array[i] = memset(array[i], ' ', 20);
array[i][20] = '\0';
i++;
}
array[i] = NULL;
shm_id = // here I get the shmid with the flag IPC_CREAT to create the shared memory
addr = shmat(shm_id, NULL, SHM_R | SHM_W);
}
但是“array[i] = memset(array[i], ' ', 20);”这一行出现了分段错误
我做错了什么?
【问题讨论】:
标签: c linux arrays unix shared-memory