sizeof() operator in array c++?

I'm learning about the sizeof() operator in c++. I know that it gives the size of an array. Why you need to know how many bytes is in an array? How does that help your program; how does that help you (the programmer)? Please explain and provide examples if possible.
Thanks!
So you can access the array within the bounds, avoiding errors.
for instance sizeof(array_of_int)/sizeof(int) gives you the number of elements in the array (array length).
Rapid Example:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

int YourArray[10] = {0};

int CompleteByteSize = sizeof(YourArray);
int NumberOfElements = sizeof(YourArray) / sizeof(*YourArray);

int Result = YourArray[0]; // First Element
Result = YourArray[10];
// Error! This is OVER the last element! It's the 11th element!
// Language still allows you to do this but it results in a crash when run!

Result = YourArray[NumberOfElements-1]; // Last Element!

/*

Valid Range of Indices:

YourArray[0]
     |
     |
     V
YourArray[NumberOfElements-1]

*/
access the array within the bounds, avoiding errors.


What does access with the bounds mean?
My question is, why would someone need to the size their array? Who cares, right? Just type the code and get the program built and turned into an application.
But I know that knowing the size does matter, I just don't understand why :( I'm just trying to learn this and understand it thoroughly. EssGeEich, thanks for the example code, and all the replies!
Let me quote something again:

Given an array named YourArray, and it's complete size, divided by it's element size named NumberOfElements,

You can only access from:

YourArray[0]

to

YourArray[NumberOfElements-1].

Accessing YourArray in a position higher than (NumberOfElements-1) results in a error.

You need to know its size to avoid such errors.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <cstdio>

int main()
{
    int YourArray[] = {0,53,512,2416,312,53};
    int ArrayNumOfElements = sizeof(YourArray) / sizeof(*YourArray);

    for(int i = 0; i < ArrayNumOfElements; ++i)
    {
        printf("%d\n",YourArray[i]);
    }

    return 0;
}


This example gets the size of YourArray, and prints all the items in the range 0 - (ArrayNumOfElements-1), who are all valid.

If you were to access YourArray[ArrayNumOfElements] or YourArray[ (Number higher than ArrayNumOfElements)] you'd get a error/crash.

You can add or remove items from YourArray with less efforts this way instead of using constants you have to remember to change if you add/remove items.
Last edited on
What does access with the bounds mean?

If you reference the 11'th element of EssGeEich's example, you're referencing an element that is out of bounds since only 10 elements were defined. Unexpected things will happen including possibly crashing your program, corrupting data, or computing the wrong answer.

why would someone need to the size their array?

What if you want to write the array to disk? You can use the sizeof() operator in the write statement:
 
  outfile.write (array, sizeof(array));





Topic archived. No new replies allowed.