non-standard / non-POSIX getcwd() not calling malloc -



non-standard / non-POSIX getcwd() not calling malloc -

what part of quote getcwd man-page misunderstanding?

char *getcwd(char *buf, size_t size); ... extension posix.1-2001 standard, linux (libc4, libc5, glibc) getcwd() allocates buffer dynamically using malloc(3) if buf null. in case, allocated buffer has length size unless size zero, when buf allocated big necessary. caller should free(3) returned buffer.

because

21 char * buffer = null; 22 size_t buffersize = 0; 23 getcwd(buffer, buffersize); 24 printf("%s\n", buffer);

is causing seg-fault @ line 24 , gdb's backtrace tells me buffer = 0x0?

edit:

getcwd(buffer, buffersize);

still doesn't work whatever reason, but

buffer = getcwd(null, 0);

does

you miss c has phone call value; not phone call reference:

getcwd(buffer, buffersize);

can not alter pointer buffer (only buffer points to, since null...). that's why need utilize value returned (this non-standard version of) getcwd.

you missed read homecoming value section of man page or misinterpreted quoted part says the caller should free(3) returned buffer. :-)

posix getcwd

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -