Jump to content
  • Advertisement
Sign in to follow this  
YellowMaple

array size/length in C

This topic is 4552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. ;)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!