tags:

views:

171

answers:

5

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!

+2  A: 

The order you declare the pointers on will determine the order they are placed on the stack in memory. My guess is that there is a buffer overrun starting at the address of buf which is trashing dir in example one but not two. You're other OS may be preventing this bug silently or handling it differently some how.

Segfault
+3  A: 

You only declared a pointer, you didn't allocate any memory for getcwd to write into. getcwd doesn't allocate the memory for you. You need to call malloc to allocate it yourself.

Steve K
+2  A: 

Order of declaration does not matter.

char *getcwd(char *buf, size_t size);  

The getcwd() function copies an absolute pathname of the current working 
directory to the array pointed to by **buf**, which is of length **size**.

buf should be allocated memory before using it.

buf = malloc(length * sizeof(char));

After char *buf; is executed, buf contains garbage-value and when getcwd() tries to modify the memory pointed bu buf, it causes Segmentation Fault.

N 1.1
+5  A: 

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).

Edit:

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 */
Nikolai N Fetissov
Thanks, NULLing it worked. I also read that getcwd() allocated memory for the buffer, so I wasn't worried about allocating it myself. I was reading up but was wondering - is there a rule for when you initialize a pointer to NULL versus waiting to assign the pointer?
chucknelson
There's no implicit rule. Each function is different - read the man page for it. Say, here the answer was easily available in `man getcwd`.
Nikolai N Fetissov
@chucknelson: Most functions won't allocate the memory for you, so the rule is, allocate the pointer yourself ahead of time. Otherwise, you are passing garbage to a function expecting a valid char*-buffer.
BlueRaja - Danny Pflughoeft
You do not need the variable `buf` at all; just write `NULL` as the first argument. Because C always passes parameters by value, any variable that you pass as a parameter to a function must be initialized first.
mark4o
+3  A: 

POSIX requires the first argument to be a pointer to a buffer in which the path name is stored. A common extension, which is present on Mac OS X, Linux, Solaris, and others, is that if the first argument is NULL then getcwd() will allocate a buffer for you using malloc() -- you would then free this using free() when you are done with it. This extension is allowed by POSIX but not required, so you should not depend on it in portable code.

In your case, you are passing an uninitialized value as the first argument to getcwd(). If it happens to be equal to NULL then a buffer would be allocated; if it is some other invalid pointer then you may get a segmentation fault. Since the value is uninitialized it could have any value, and could depend on the order of declaration. If you intended for a buffer to be allocated by getcwd() then pass in the value NULL explicitly; it is not necessary to declare a variable for this. However a more portable solution would be to pass in a pointer to your own buffer rather than relying on this extension.

mark4o