Thread: finding size of integer array?

Page 2 of 2 First 12
1. No Profile Picture
Offensive Member
Devshed Novice (500 - 999 posts)

Join Date
Oct 2002
Location
in the perfect world
Posts
622
Rep Power
31
If you are declaring the array

float fArray[]={1,2,3,4,5};

why do you need to find its size?
(as you hard coded its size in the first place)

why not

#define ARRAY_ELEMENTS 5
float fArray[ ARRAY_ELEMENTS] = { 1,2,3,4,5 };

for(x=0;x< ARRAY_ELEMENTS ;x++)
2. No, I disagree. If you're going to use macros, make them flexible. Scorpions4ever already gave a better alternative (twice!):

Code:
`int y = sizeof (x) / sizeof (x[0]);`
You can do that with a macro as well:

Code:
```#define NUM(v) (sizeof(v) / sizeof((v)[0]))
...
int a[] = {5, 4, 3, 2, 1};
int y = NUM(a);```
Just make sure you only use it on fixed-size arrays. :D
3. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16
or even better possibly, get the computer to count the array for you and use that number, so if you change the array in the code you don't have to also change the define as it's automatic
Code:
```#define ARRAY_ELEMENTS sizeof(fArray)/sizeof(fArray[0])
float fArray[] = {1, 2, 3, 4, 5};

for(x = 0; x < ARRAY_ELEMENTS ; x++)```
the "sizeof(whatever)" parts gets substituted with the values at compile time apparently - so it's not less efficient. depending on your compiler though, the ARRAY_ELEMENTS in the end code will either end up for example like 20/4, or 5

i wrote that before i saw analyser's post :/
Last edited by balance; June 17th, 2003 at 06:31 AM.
4. No Profile Picture
Offensive Member
Devshed Novice (500 - 999 posts)

Join Date
Oct 2002
Location
in the perfect world
Posts
622
Rep Power
31
>>No, I disagree. If you're going to use macros, make them flexible.

You miss my point. If you KNOW the size of the array (as you hard coded the number of elements) why do you need to dynamically find its size?

If you went

char sBuffer[64];

would you then try to calculate its length?

And if you are doing it on dynamically allocated arrays you will ned to use something like (in WIN32)

dwSize = GlobalSize( GlobalHandle( fArray ));

>>#define ARRAY_ELEMENTS sizeof(fArray)/sizeof(fArray[0])
float fArray[] = {1, 2, 3, 4, 5};

AFAIK you need to do this more like

#define ARRAY_SIZE( Array ) ( sizeof( Array ) / sizeof( Array[0] ) )
Last edited by TechNoFear; June 17th, 2003 at 10:25 PM.
5. If you went

char sBuffer[64];

would you then try to calculate its length?
If I'm writing a quick hack for something with only one or two fixed-size arrays, maybe. But if I have, say, ten or twenty fixed-size arrays, then I don't want my code to be cluttered with lots of #define ARRAY_1_SIZE macros.

If you KNOW the size of the array (as you hard coded the number of elements) why do you need to dynamically find its size?
Because I don't make the assumption that the chosen size will never change. If I have an input buffer of 1024, and discover after some time that performance increases when I change it to 2048, then I won't have to change my macro.

Now, granted, if you're keen on using

#define ARRAY_SIZE (sizeof(v) / sizeof(v[0])

then you won't have to change any macros either. My point is merely that your code is littered with #defines, one for each fixed-size array. I try to strive for clean, clear code, and personally I think having all those #defines works against that.

But that's my opinion :)
6. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16
>>#define ARRAY_ELEMENTS sizeof(fArray)/sizeof(fArray[0])
float fArray[] = {1, 2, 3, 4, 5};

AFAIK you need to do this more like
#define ARRAY_SIZE( Array ) ( sizeof( Array ) / sizeof( Array[0] ) )
the first way there works fine. there's obviously 2 ways of doing it. i think the second one will work with any array where as the first define will work only specifically with fArray in that example.

i think using one of these defines to get an array size is a good idea because
- computers are much better at counting than i am
- if you're going to change an array- there's only one change needed, the array change itself, rather than two changes.
- this is all completely 'free' as in no extra efficiency cost because the array size calculations happen at compile time.

i think this only applies to when you're filling an array's elements up right at the start though, not dynamically, so this allows the elements themselves to dictate the size rather than anything else.

if you used

char sBuffer[64];

and were planning on filling that array dynamically later, then you have to specify the size yourself like that as the elements aren't in the array yet to dictate the size.

so what i'm saying is if you were using this definition (or is a decleration?)

char sBuffer[64];

and were filling it up at a later point and envisaged / planned on having 64 elements at the start, you wouldn't be able to use a sizeof define there. you wouldn't have the option. i don't think so, in any case.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2002
Posts
272
Rep Power
22
You can still use the sizeof method. The array is a fixed size regardless of when you "fill it up". In the case where you don't specify the size but initialize it you are effectively fixing the size with the number of initializers. The only time you can't use the sizeof method is when you use malloc/calloc/new to allocate the space for the array.
8. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16
In the case where you don't specify the size but initialize it you are effectively fixing the size with the number of initializers.
i know. i'm talking about when you don't do that. using sizeof's in the define instead of stating the size yourself in the code, like talked about above, is only applicable to situations like:

int array[] = {1, 2, 3, 4, 5};

not situations like:

int array[5];

// then, for example, use a for loop to fill array later in the code.

you wouldn't be able to use sizeofs in place of 5 in a #define (because it's got nothing to count), nor would you want to. you'd just use a straight normal define for the second example, obviously.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2002
Posts
272
Rep Power
22
Maybe I am missing your point. I would still use a #define that uses the sizeof method later in my code so that I would only have to change the array definition and not the code if I need to grow/shrink the array.

Code:
```int array[5]; // or int array[] = { 1, 3, 5, 7, 9 };

#define ARRAY_SIZE (sizeof array/sizeof array[0])

for (i = 0; i < ARRAY_SIZE; i++)
{
// Some code here, perhaps initializing array
}```
10. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16
Code:
```/* 1 */

int array[5];

#define ARRAY_SIZE (sizeof array/sizeof array[0])```
only this:
Code:
```/* 2 */

#define ARRAY_SIZE (sizeof array/sizeof array[0])

int array[] = { 1, 3, 5, 7, 9 };```
i was saying using int array[5]; in conjunction with sizeof's in the define will not work and is only applicable to int array[] = { 1, 3, 5, 7, 9 }; situations. but obviously not :)

but a disctinction between the two types of arrays is still worth pointing out, and still stands i think, with regards to the usefulness of sizeof's in the define.

for /* 1 */ you could have just as easily used no sizeofs, like:
Code:
```#define ARRAY_SIZE 5

int array[ARRAY_SIZE];```
with no real loss nor gain. much the same. still just 1 number to change if you're expanding or reducing the size of the array.

but with /* 2 */, without sizeof's, you'd have to use an extra hard coded number. sizeof's with that type of array, gives a tangable extra advantage.

so, in my opinion, sizeof's with an array defined like int array[] = { 1, 3, 5, 7, 9 }; are well worth using. but using sizeof's with other types of arrays doesn't make much difference.

it's that distinction i was trying to get at. in the conversation above, it went from array[] = { 1, 2, 3, 4, 5 }; style array, to array[5]; style without any mention of or distinction between the two.
11. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2002
Posts
272
Rep Power
22
I think I understand what you mean now. A point I would like to make is that there is no fundamental difference between the two other than the initializers. They are both just definitions of an array of five ints.
Page 2 of 2 First 12