It has to do with arrays, pointers and the meaning of sizeof
.
char dir[512];
sizeof(dir);
This is asking for the size of an array, which in this case is sizeof(char) * 512
-> 512
.
char *dir;
sizeof(dir);
This is asking for the size of a pointer, which in this case is sizeof(char *)
-> 4
or 8
char *dir;
sizeof(*dir);
This is an odd construct. I really don't feel comfortable with it. Because dir
has not been initialized any dereferncing of dir
is cause for concern. But I think the compiler is working out the correct thing here, which in this case is sizeof(char)
-> 1
.
When working if pointers, you need to carry around the size which you allocated.
char*dir;
if(ac > 2)
{
dir = malloc(sizeof(char) * 512);
getcwd(dir, sizeof(char) * 512);
printf("dat dir is:\n");
printf("%s\n", dir);
}
The right answer™ is a bit more complicated.
if (ac > 2)
{
size_t size = 512;
char *buf = malloc(size);
char *dir = getcwd(buf, size);
while (dir == NULL && errno == ERANGE)
{
size *= 2;
buf = realloc(buf, size);
dir = getcwd(buf, size);
}
printf("dat dir is:\n");
printf("%s\n", dir);
free(buf);
}
First thing, I drop sizeof(char)
. In C, sizeof(char)
is defined to be 1
, so it's redundant. Next, set dir
as the return value of getcwd(3)
and keep the data buffer separate. Be sure to check the return value, if getcwd(3)
returns NULL
and errno
is ERANGE
then buf
was not big enough. In my example, I double the size of the buffer before trying getcwd(3)
again. Finally, don't forget to free buf
.
The thing I'm not doing here is checking the return value of malloc(3)
or realloc(3)
. This is because I feel there is not point in this example. If I can't allocate the size of a path, then my program is going to crash no matter what I do.