....大家好,我在运行代码时总是收到“分段错误”。我知道当写入文件出现问题时会发生此错误(我想共享内存也是如此),我知道错误来自 for 循环,我尝试了所有方法来解决此错误但失败了(我什至删除了 for 循环,只输入 *s = 'A')。请帮忙。
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
main()
{
//Shared memory parameters
int shmid ;
int shmsize = 14;
key_t key = 2121;
char *shm, *s;
//Create SMS
if ((shmid = shmget (key , shmsize, 0666 | IPC_CREAT)) == -1) {
perror ("Error in Creating the SMS");
abort();
}
//Attatching the sms to the address space
if (shm = shmat(shmid , NULL , 0) == (char *)-1) { /*<<<< 23 */
perror ("Error in attatching the SMS");
abort();
}
int i ;
s = shm;
for(i = 0 ; i <= 63 ; i++)
*s++ = (char)i;
*s = NULL; /*<<<< 33 what's the problem */
}
我也在 23 和 33 收到警告