写入共享内存核心转储分段错误
writing to shared memory core dump segmentation fault
我需要写入共享内存,因此我有
#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!
}
它在 strcat
中抛出异常,如果我删除它,异常会在 strcpy
的下一行抛出。
我需要写入内存“b34[=13=]
”(4 个字符)然后读取它。
这个:
strcat(userInput, '[=10=]'); //THROWS EXCEPTION!!
不是有效的 C,它也不是 "throw an exception" 因为 C 没有例外。也许它会崩溃,但无论如何这应该是预料之中的,因为您甚至没有编写有效的代码。使用拒绝像这样明显无效代码的编译器。
编辑:和这个:
char* userInput = malloc(5);
read(0, userInput, 3);
strcpy(shmaddr,userInput);
无效,因为你读取了三个字符,留下 userInput
中的最后两个字符未初始化,然后你调用 strcpy()
从 userInput
读取空终止字符串,但你有没有以 null 结尾的字符串,因此它是未定义的行为,任何事情都可能发生——包括崩溃。所以试试这个:
const size_t INPUT_MAX_SIZE = 3;
char userInput[INPUT_MAX_SIZE + 1];
read(STDIN_FILENO, userInput, INPUT_MAX_SIZE);
userInput[INPUT_MAX_SIZE] = '[=12=]'; // add null terminator
strcpy(shmaddr,userInput);
或者更好:
read(STDIN_FILENO, shmaddr, INPUT_MAX_SIZE);
即直接读取到目的地,而不是临时缓冲区。
函数 strcat
和 strcpy
都期望参数是空终止字符串,在你的情况下, userInput
或 shmaddr
都不满足这个条件,那就是为什么你看到程序崩溃。试试这个:
#define FLAGS IPC_CREAT | 0644
int main(void) {
key = ftok("ex31.c", 'k');
shmid = shmget(key, 4, FLAGS); // the buffer needs at least size 4 to hold the 3 char string and the null terminator
shmaddr = shmat(shmid, 0, 0);
char* userInput = malloc(5);
read(0, userInput, 3);
userInput[3] = '[=10=]';
strcpy(shmaddr, userInput);
}
我需要写入共享内存,因此我有
#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!
}
它在 strcat
中抛出异常,如果我删除它,异常会在 strcpy
的下一行抛出。
我需要写入内存“b34[=13=]
”(4 个字符)然后读取它。
这个:
strcat(userInput, '[=10=]'); //THROWS EXCEPTION!!
不是有效的 C,它也不是 "throw an exception" 因为 C 没有例外。也许它会崩溃,但无论如何这应该是预料之中的,因为您甚至没有编写有效的代码。使用拒绝像这样明显无效代码的编译器。
编辑:和这个:
char* userInput = malloc(5);
read(0, userInput, 3);
strcpy(shmaddr,userInput);
无效,因为你读取了三个字符,留下 userInput
中的最后两个字符未初始化,然后你调用 strcpy()
从 userInput
读取空终止字符串,但你有没有以 null 结尾的字符串,因此它是未定义的行为,任何事情都可能发生——包括崩溃。所以试试这个:
const size_t INPUT_MAX_SIZE = 3;
char userInput[INPUT_MAX_SIZE + 1];
read(STDIN_FILENO, userInput, INPUT_MAX_SIZE);
userInput[INPUT_MAX_SIZE] = '[=12=]'; // add null terminator
strcpy(shmaddr,userInput);
或者更好:
read(STDIN_FILENO, shmaddr, INPUT_MAX_SIZE);
即直接读取到目的地,而不是临时缓冲区。
函数 strcat
和 strcpy
都期望参数是空终止字符串,在你的情况下, userInput
或 shmaddr
都不满足这个条件,那就是为什么你看到程序崩溃。试试这个:
#define FLAGS IPC_CREAT | 0644
int main(void) {
key = ftok("ex31.c", 'k');
shmid = shmget(key, 4, FLAGS); // the buffer needs at least size 4 to hold the 3 char string and the null terminator
shmaddr = shmat(shmid, 0, 0);
char* userInput = malloc(5);
read(0, userInput, 3);
userInput[3] = '[=10=]';
strcpy(shmaddr, userInput);
}