Array element count exceeds what it has to be

#include <iostream>
using namespace std;
int main()
{
	int number[20];

	for (int i = 0; i < 20; i++)
	{
		number[20] = (int)(i * 10);
		cout << "number[" << i << "]:" << number[20] << endl;
	}
	cout << number[20];


	cout << sizeof(number);

	return 0;

}

PRINTS:

number[0]:0
number[1]:10
number[2]:20
number[3]:30
number[4]:40
number[5]:50
number[6]:60
number[7]:70
number[8]:80
number[9]:90
number[10]:100
number[11]:110
number[12]:120
number[13]:130
number[14]:140
number[15]:150
number[16]:160
number[17]:170
number[18]:180
number[19]:190
19080

this code prints the number of this array’s elements as 80. How can I fix it?

You have created an array with the size of 20 elements, but the array starts at 0, which is the first element. This means that number[19] is the last element in your array, not number[20].

Basically you overwrite memory at a position that is not intended to be used by your array. While arrays in c/c++ are fast they also have no system that will prevent you from doing so. You code may work at the moment but actually its behavior is undefined. This is a quite common bug. For more information check out this link:

https://www.imperva.com/learn/application-security/buffer-overflow

What you probably wanted to do is this: (Note the difference in the usage of the letter i)

#include <iostream>

int main()
{
    int number[20];
    for (int i = 0; i < 20; i++)
    {
	    number[i] = i * 10;
	    std::cout << "number[" << i << "]:" << number[i] << std::endl;
    }
    return 0;
}
1 Like
  1. Not sure why there’s a C-style cast.
  2. sizeof is returning the size in bytes. int is typically 4 bytes so 4 * 20 = 80.
  3. Just use C++
#include <array>
//...

std::array<int, 20> number;
for (int i = 0; i < number.size(); i++)
{
	number[i] = i * 10;
	std::cout << "number[" << i << "]:" << number[i] << std::endl;
}
	
std::cout << '\n' << number.back() << '\n' << number.size();
1 Like

However this does not solve the issue of total number of elements being 80 in this array.

#include<iostream>
using namespace std;
int main()
{
   int number[20];

   for (int i = 0; i < 20; i++)
   {
   	number[19] = (int)(i * 10);
   	cout << "number[" << i << "]:" << number[19] << endl;
   }
   cout << number[19];


   cout << sizeof(number) / sizeof(int);

   return 0;

}

cout << sizeof(number)/sizeof(int); just writing this is enough to get 20. thank you for your help.

I dont know why this web page doesnt show < I O S T R E A M > writing with lower case, no blanks.

That code you just posted is still incorrect re number[20] everywhere instead of just the initial declaration.


To use a code block surround it in 3 back ticks or highlight it and press the </> button.

example of the backticks option:
```
code
```

I dont know why this web page doesnt show < I O S T R E A M > writing with lower case, no blanks.

Because of tags. You need to use a code block or if it’s a single thing <like this> just single backticks on either side `<like this>`

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms