Counting Program

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:

#include "stdafx.h"
#include <iostream>

using namespace std;

void printarray (int arg[], int length) {
for (int n=0; n<length; n++)
cout << arg[n] << " ";
cout << "\n";
}

int counter = 0, result = 0;
int cool[0x9FFFFFF];

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.
In case it wasn't clear, ascii is talking about the size of 'cool'.
Yeah sorry if I didn't make that clear :o
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.
Try a million.
But isn't 0x9FFFFFF bigger than a million? I just wanted to get the highest number possible without errors for my array lol.
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.
Last edited on
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.
where to use _tmain anyway? can anybody give a complete explanation?
Topic archived. No new replies allowed.