On a Solaris 5.8 machine, I have the following code:
[non-working code]
char *buf;
char *dir;
size_t psize;
psize = (size_t) 1024;
dir = getcwd(buf, psize);
On this unix machine, the above does not work and I get a segmentation fault when trying to run the program. It only works if I declare dir
before buf
:
[working code]
char *dir;
char *buf;
...
dir = getcwd(buf, psize);
When using another flavor of Unix, such as Mac OS X, I don't get any of these what seem to be very strict rules on how to write the code. Can anyone explain what's going on with the above example? Thanks!
Here's from getcwd(3)
:
DESCRIPTION The getcwd() function copies the absolute pathname of the current working directory into the memory referenced by buf and returns a pointer to buf. The size argument is the size, in bytes, of the array referenced by buf. If buf is NULL, space is allocated as necessary to store the pathname. This space may later be free(3)'d.
That is - set the buf
to NULL
and free(3)
the dir
when done; OR allocate space for the buf
yourself (since you are telling the getcwd(3)
you have 1K there).
So to clean it up a bit, it's either:
char *dir = getcwd( NULL, 0 );
if ( dir == NULL ) { /* handle error */ }
/* use dir */
free( dir );
or
char buf[1024]; /* or allocate it with malloc(3) */
if ( getcwd( buf, 1024 ) == NULL ) { /* handle error */ }
/* use buf, DO NOT free it if it's on the stack or static,
only if malloc-ed */