It doesnt work, always say: program.exe has stopped working

#include <iostream>

using namespace std;

#define N 1000001

int main()
{
long long v[N]= {0},i;
for(i=1; i<=N; i++)
cout<<v[i]<<' ';
return 0;
}
Best guess, stack overflow. What are you trying to accomplish?
I'm trying to do a vector with all v[i=1,2,4,8,16..]=1, and in rest 0, up to 1 000 000.
Someone have any idea?
Last edited on
Try to declare your array as static or use a std::vector.
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

Also be aware that the last valid index is N -1, not N.
Dumping a million numbers on the console might take a long time - maybe do it before your lunch break. :)
You're trying to create an array 7.5 MB in size, on the stack memory.

Under windows, the stack memory for a process is typically 1MB. As Heintzman says, you're overflowing the stack. You're trying to use more stack memory than you have been allocated.

Following on from Thomas, std::vector sounds like what you need.

I'm trying to do a vector with all v[i=1,2,4,8,16..]=1, and in rest 0, up to 1 000 000.
Someone have any idea?


1
2
3
4
5
6
7
8
std::vector<int> bigBinary(1000000, 0);  // Create vector, size 1000000, all zeros

int i = 1;
while (i < 1000000)
{
  bigBinary[i] = 1;
  i = i*2;
}


Edited for missing template

Last edited on
Thanks, but one question for me..
What's the difference between std::vector and an vector from int?
int a[50]; is a C style array, no management, you have to take care of allocating and bounds checking yourself.

std::vector<int> b(50); is a library class that does all of the management for you.

std::vector has operator overloads so it can look like a C style array.
1
2
a[1] = 5;
b[1] = 5; // this is really a function call to the [] operator in std::vector 


Don't call arrays vectors, they are different things, in most cases you should use a vector over a C style array.

there is also std::array which i use quite a lot.
http://www.cplusplus.com/reference/array/array/
Topic archived. No new replies allowed.