Hello everybody! I am new to this forum, and also pretty new to C++. I was wondering if you guys could help me with my project. I decided to play around with arrays, and created this counting program, however, I'm having trouble with the max size of the array. I tried UINT_MAX, but the size was too big apparently. This is the code I have so far:
int _tmain(int argc, _TCHAR* argv[])
{
cout << "What number would you like to count to? ";
cin >> counter;
for (int n=0; n<counter; n++)
{
result = result + 1;
cool[n] = result;
}
printarray (cool, counter);
system("pause");
return 0;
}
Please note that I'm still a beginner, therefore it may not be the best coding in the world.
167772159 multiplied by 4 (because there are four bytes in an int) is 671088636, which is more than half a gigabyte. In other words, you're allocating a crapload of memory there. Knock down the size of your array by a lot there and you shouldn't get any errors. Also, why do counter, result and cool have to be global variables? If you can try to avoid global variables as much as you can.
Well, when I compiled it and ran it, it ran fine, it's just that I was wondering how big the size of cool should be. When I put in UINT_MAX as the size of cool, it gave me an error saying that the number can't be bigger than 0x7FFFFFFF, so I just played around with it until I got 0x9FFFFFF. And could you tell me what's better to use than int? I thought it would be fine for this since I'm using integers.
If you want the highest number of elements possible dynamically allocate the array, you can have much larger arrays this way. I'm saying a million because you should never allocate an array that large.
Hm, than how can I make it fit the size of counter? I tried doing int cool[counter], but that didn't work for me lol. And I tried int cool[sizeof(counter)], but that crashed when i went to 1000, even though it doesn't crash if I put in a specific allocation number.