Possible Duplicate:
Segmentation fault on large array sizes
Hi all
I am trying to create a very big array in the VS 2010 using C++.
When I try to create a array like below
int dp[4501][4501]
or
int dp[1000][1000]
It threw an exception "Stack Overflow" Then I change it to:
int dp[100][100]
everything is fine.
So if I want to create a big array like above, what should I do?
Best Regards,
Usually you need to create such an array dynamically on the heap. int *integer_array = (int*)malloc(2000000 * sizeof(int)); float *float_array = (float*)malloc(2000000 * sizeof(float)); The array might be too large for stack allocation, e.g. if used not globally, but inside a function.
Arrays are static so you won't be able to change it's size. You'll need to create the linked list data structure.
In case it isn't clear, your array cannot be of double length. It's undefined behavior. This is because a double is not an integer, but a rational number that could be an integer.
Array Initialization in Java int[] intArray = new int[10]; This allocates the memory for an array of size 10 . This size is immutable. Java populates our array with default values depending on the element type - 0 for integers, false for booleans, null for objects, etc.
If you want to avoid new[]
, or avoid using std::vector
, make the array global. This will put the array on heap and stack overflow will not occur.
Use dynamic allocation or the STL. There was a recent thread about a very similar question. See this.
You should use dynamic allocation:
typedef std::vector<int> int_vector;
int_vector dp(10000);
A double array can be simulated by nesting arrays:
typedef std::vector<int_vector> int_double_vector;
int_double_vector dp(4501, int_vector(4501));
Put it on the heap.
Text from Parashift faq : Why should I use container classes rather than simple arrays?
EDIT:
Take a look at stackoverflow threads:
When would you use an array rather than a vector/string? Why use iterators instead of array indices?
Your stack has overflowed with too many bits. You must drain them. Preferably onto a heap of other bits. I suggest /F67108864. The /F stands for "F'ing hell why is the stack so small compared to the heap?". The 67108863 is arbitrary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With