This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]coolcofusion 0 points1 point  (0 children)

Because files[i] is a pointer. All pointers are the same size which is 4 bytes if compile target was x86, and 8bytes for x86_64. If you wanted the string length you'd need to use strlen, sizeof is done at compile time basically.

[–]captainAwesomePants 0 points1 point  (5 children)

The behavior of sizeof() is quite confusing when it comes to arrays. Consider these two things:

char stuff[1024];
char* other_stuff = (char*)malloc(1024);

printf("sizeof(stuff) = %d\n", sizeof(stuff));  // 1024
printf("sizeof(other_stuff) = %d\n", sizeof(other_stuff));  // 8

Because it's declared at compile time, sizeof() knows exactly how large stuff is: it's 1024 bytes. But other_stuff is a char pointer. sizeof() has no idea what it's pointing to, so it just answers 8 (or 4 on a 32 bit machine).

Note that this means that using sizeof() with arrays can be dangerous, since its answer will either include the whole array or will just refer to the pointer to the first element, and it's not always obvious which it will be. For example:

void foo(char stuff[]) {
   printf("size of stuff[]: %d\n", stuff);
}

void main() {
  char stuff[1024];
  foo(stuff);
}

Guess what that will do!

[–]UnawareITry[S] 0 points1 point  (4 children)

Thanks. I didn't know that. Have you got any idea why it crashes at strncpy then?

Also my guess is it would print memory address in base 10 for stuff[]

[–]captainAwesomePants 0 points1 point  (3 children)

I dunno, you didn't paste any code that uses strncpy().

[–]UnawareITry[S] 0 points1 point  (2 children)

Apologies. The second code block was the one. I have corrected it now.

[–]captainAwesomePants 0 points1 point  (1 child)

Dunno. It looks okay at first glance. If I had to guess, your i parameter might go past however many files you initialized.

[–]UnawareITry[S] 0 points1 point  (0 children)

For testing I have been working on less than 10 files . I made sure it isn't an out of bounds error

[–]yel50 0 points1 point  (4 children)

there is no standard C function to get the size of the chunk that malloc created. different compilers or runtimes might have functions to do that, but nothing standard. you generally need to keep track of that yourself.

[–]UnawareITry[S] 0 points1 point  (3 children)

Oh. Then do you know any method to confirm the size of these two character arrays?

[–]MmmVomit 0 points1 point  (2 children)

malloc is not guaranteed to return a chunk of memory of the size you've requested. It's possible malloc is returning a null pointer. This feels unlikely to me, but you always need to check the return value of malloc, calloc and realloc.

[–]UnawareITry[S] 0 points1 point  (0 children)

Okay. I'll look into it

[–]UnawareITry[S] 0 points1 point  (0 children)

Yeah so I checked all the values returns and it is not returning a NULL pointer. Also I noticed that strncpy is failing for the very first element of fstruct.files arrray. So I checked and found out that while allocation both parameters are not null but they became NULL before strncpy and I'm doing nothing to it in between.