....嗨,大家好,我總是在運行我的代碼時出現「分段錯誤」。我知道這個錯誤發生在寫入文件時出現問題(我想共享內存也是如此),我知道錯誤來自於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