What is size of integer?
The size of an int
is really compiler dependent. Back in the days when processors used to be 16 bit, an int
was 2 bytes. Nowadays, it's most often 4 bytes on a 32-bit as well as 64-bit systems.
The best way of checking size of int
is
cout << sizeof(int);
Assumption: sizeof(int) = 4 bytes in your architecture.
What you tell compiler?
When you declare a statement
int array[10];
You tell Compiler to reserve 40 BYTES for use(based on Assumption above.)
What you do to the allocated memory?
You are using only 9 elements (1-9) thats 36 bytes of 40 allocated wasting 4 bytes of memory. Just memory is cheap doesn't mean its free. Morever if your working with 3 dimensional arrays.
Example:
int array[100][100][100]
You will end up losing a lot of memory I mean lot of memory that other process require but can't use just because you have deliberately reserved it and not used.
Problem with Strings.
If you are working with character arrays, you may something land up in disaster.
Example:
char str[8];
str[1] = 'H';
str[2] = 'e';
str[3] = 'l';
str[4] = 'l';
str[5] = 'o';
str[6] = '\0';
cout<<str;
There maybe situation where no output will be displayed as the str[0]
may contain the NULL
character.