我误解了 getcwd 手册页中这句话的哪一部分?
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 行导致 Seg-Fault 并且 gdb 的回溯告诉我缓冲区 = 0x0?
编辑:
getcwd(buffer, bufferSize);
无论出于何种原因仍然无法正常工作,但是
buffer = getcwd(NULL, 0);
做