Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with array size

Tags:

c++

arrays

sizeof

I happened to ask myself a question about arrays in c++. Well, we all know that arrays are fixed collections of something, I say fixed because it is necessary to declare array length when defining arrays. Well, let's consider an example:

char myarray[10] = {'\0'};
int sz = sizeof(myarray); // It is supposed to be 10

Well, it is correct, 10 is the number returned by sizeof. This can be done by the compiler because he knows how much space it placed for that variable.

Now consider what happens in this situation:

void dosome(mystruct* arr) {
   int elements = sizeof(arr)/sizeof(mystruct);
   for (int i = 0; i < elements; i++) {
      // Do something hoping no overflow will ever occur
   }
}

Nice... but I suppose it can be overflow prone. If I pass to this function an array I created in a "normal" way, everything should be fine:

mystruct array[20];
dosome(array);

No problem. But if I do this:

mystruct* array = (mystruct*)malloc(80*sizeof(mystruct));
dosome(array);

WHAT HAPPENS??????????????????? I would like to understand how sizeof behaves, this function is evaluated at compile time right??? ok, what happens when I use not an array, but something very cumbersome like a block of data like that one? furthermore, I could realloc it woth another call to malloc and ask to dosome to process that datablock again. Will it work? I could try it physically, but I would get some exact answer about the behavioir of sizeof.

Thank you.

like image 886
Andry Avatar asked Jun 28 '26 17:06

Andry


1 Answers

it's wrong starting from the mystruct array[20] example. Because the function receives a pointer type, and not an array type, it cannot deduce the number of elements in the array. you are actually getting the size of a mystruct* when you perform sizeof(arr).

You can use templates to write functions which take arrays as parameters, but the suggested way in C++ is to use vectors, if I am not wrong.

The "way" to receive arrays as parameters would be to write something like:

template <int N> void somefunction(int (&v)[N]);

EDIT corrected the function declaration. oops.

like image 50
lijie Avatar answered Jun 30 '26 09:06

lijie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!