我需要写共享内存,因此我有
#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个字符),然后读取它.