小编Zoh*_*gov的帖子

写入共享内存核心转储分段故障

我需要写共享内存,因此我有

#define FLAGS IPC_CREAT | 0644
int main() {
key = ftok("ex31.c", 'k');
shmid = shmget(key, 3, FLAGS);
shmaddr = shmat(shmid,0,0);    // THOSE LINES WORK AS EXPECTED

char* userInput = malloc(5);
read(0, userInput, 3);  // I want to read "b34" for example, WORKS GOOD
strcpy(shmaddr,userInput);   // THROWS EXCEPTION!
}
Run Code Online (Sandbox Code Playgroud)

它会抛出异常strcat,如果我删除它,则会在下一行抛出异常strcpy.我需要写入内存" b34\0"(4个字符),然后读取它.

c malloc coredump shared-memory backslash

-2
推荐指数
1
解决办法
228
查看次数

标签 统计

backslash ×1

c ×1

coredump ×1

malloc ×1

shared-memory ×1