....嗨,大家好,我总是在运行我的代码时出现“分段错误”。我知道这个错误发生在写入文件时出现问题(我想共享内存也是如此),我知道错误来自于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
您可以添加到你的回答中:shm的大小是14,循环是63字节...... – Aubin 2013-02-23 16:33:03
非常感谢,下次我会小心的。 – 2013-02-23 17:02:28