我在C中遇到了一個有趣的問題,當從main調用一個外部方法時,它嘗試創建一個通過引用傳遞給它的本地(主)字符串。如果我在main中創建字符串,它會按預期工作,但如果我調用外部方法,它會因segfault失敗。 Valgrind的關於訪問所述的strtok結果的第一個元素輸出:strtok在本地變量C
Address 0xfffffffffeffebc0 is not stack'd, malloc'd or (recently) free'd
--- --- test.c的
extern void tt(char* x);
main(argc, argv)
int argc;
char *argv[];
{
char line[5000];
// if I uncomment the following two lines and comment above it works as expected
// char * line;
// line = malloc(5000);
strcpy(line, "hello world");
printf("%d\n", sizeof(line));
tt(line);
}
--- --- test2.c中
void tt(char* l) {
char* x = strtok(l, " \t");
printf("%p\n", x);
printf("%c\n", x[0]);
}
編譯通過
gcc -c test2.c -o test2.o
gcc test.c test2.o
恕我直言,它應該打印出來像這樣:
./a.out
0x.....
h
但是,而不是我打印「h」的segfault。
有人可以解釋這種行爲嗎?
謝謝。
不是downvote的原因,但是您應該完全停止使用K&R樣式函數定義。 – 2013-05-14 16:05:59
在http://codepad.org/7KUrMWdl – dchhetri 2013-05-14 16:06:58
@ticcky上運行,爲什麼你不通過調試器並找出它崩潰的線路,以及爲什麼 – dchhetri 2013-05-14 16:08:29