array size/length in C

Started by
17 comments, last by YellowMaple 18 years, 3 months ago
Does anyone know how to check for the length of an arbitrary array in C? I used to know this... I think it was something like: #define length(a) (sizeof(a)/sizeof(a[0])) however, the above hasn't worked out so well when I tried it so I'm guessing it's incorrect :p Any ideas?
Advertisement
It can't be done. You can use that trick only to account for the length of a statically allocated array within the scope in which it was allocated.
Quote:Original post by Oluseyi
It can't be done. You can use that trick only to account for the length of a statically allocated array within the scope in which it was allocated.


This is correct, to add to the above: the actually variable types are different between static and dynamic arrays. static arrays are known to the compiler as an array, but a dynamic array is known as a pointer. I've never been a 100% certain about all of this, never seen really good documentation either.

void foo()
{
int a[16];
int * b = malloc(sizeof(int) * 16);
}

"a" is of type int[16] and "b" is of type int*... this explains why that length() macro doesn't work.
People fear what they don''t understand, hate what they can''t conquer!
You could check how much space one entry takes, and then simply count the number of entries in the array and multiply with the size of one. ;)
"Game Maker For Life, probably never professional thou." =)
Quote:Original post by Rasmadrak
You could check how much space one entry takes, and then simply count the number of entries in the array and multiply with the size of one. ;)


Just for clarification, the OP want's to find the number of entries, so he cannot use that method [wink]
Quote:Original post by Drew_Benton
Just for clarification, the OP want's to find the number of entries, so he cannot use that method [wink]

Actually, he can - if he appends an "end of sequence" identifier to every one of his arrays. That, after all, is how C strings and strlen work...

[Edit:] Alternately, he could prepend length information, so that his custom routine doesn't need to traverse the array to determine length. That's how Pascal strings work. Ultimately, though, it's better to use std::vector if C++ is an option. The benefits are many, well beyond trivial sequence length calculation.
Thanks for the replies guys. I guess it can't be done :( or the closest way to doing it would be to use an identifier like Oluseyi mentioned. Thanks!
Typically, you'll define an "array" as a struct, that contains both the count (and possibly a separate logical vs physical count), and a pointer to the data. This has the added benefit that if you resize the array data, the pointer to the array doesn't change.
enum Bool { True, False, FileNotFound };
If it was an array of structs or classes wouldn't sizeof() return the padding between members also? Just asking for my own sanity. I just woke up, but I should know the answer and it's not coming to me right away.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

The sizeof a struct includes the padding, thus the division works out right.

This topic is closed to new replies.

Advertisement