Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a big array in C++ [duplicate]

Tags:

c++

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,

like image 216
Yongwei Xing Avatar asked Jun 29 '10 03:06

Yongwei Xing


People also ask

How do you create a large array?

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.

Can you increase array size in C?

Arrays are static so you won't be able to change it's size. You'll need to create the linked list data structure.

Can C double array?

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.

How do you declare an array of size 10 10?

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.


6 Answers

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.

like image 117
Donotalo Avatar answered Oct 02 '22 23:10

Donotalo


Use dynamic allocation or the STL. There was a recent thread about a very similar question. See this.

like image 33
vpit3833 Avatar answered Oct 03 '22 01:10

vpit3833


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));
like image 26
GManNickG Avatar answered Oct 02 '22 23:10

GManNickG


Put it on the heap.

like image 23
Marc Bollinger Avatar answered Oct 02 '22 23:10

Marc Bollinger


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?

like image 42
KV Prajapati Avatar answered Oct 03 '22 01:10

KV Prajapati


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.

like image 27
John Avatar answered Oct 02 '22 23:10

John