我誤解了getcwd手冊頁的引用部分?非標準/非POSIX getcwd()不能調用malloc
char *getcwd(char *buf, size_t size);
...
As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5,
glibc) getcwd() allocates the buffer dynamically using malloc(3) if buf
is NULL. In this case, the allocated buffer has the length size unless
size is zero, when buf is allocated as big as necessary. The caller
should free(3) the returned buffer.
因爲
21 char * buffer = NULL;
22 size_t bufferSize = 0;
23 getcwd(buffer, bufferSize);
24 printf("%s\n", buffer);
在第24行和gdb的回溯導致賽格故障告訴我,緩衝=爲0x0?
編輯:
getcwd(buffer, bufferSize);
仍然沒有出於某種原因的工作,但
buffer = getcwd(NULL, 0);
確實
AAAAAAAAAAAAAAAAAHHHHHHHHHHHHHHHHH,是的,你是正確的,這就是爲什麼getline和getdelim需要char **緩衝區... – Kdawg 2014-10-10 11:03:11
@Kdawg正確。如果您認爲此答案有幫助,下一步將是點擊向下箭頭和/或下面的複選標記以「接受」答案。 – Jens 2014-10-10 12:56:06
抱歉我的男人,「投票要求15聲望」...有一天我會到達那裏 – Kdawg 2014-10-10 13:43:39