0

I need to create an array like this

double grid [15000][40];

but the stack in Visual Studio 2012 is only of 1MB. How can I use variables like this or bigger? This mean that if I create a

std::vector<int>

and I push_back 600 000 times it goes in stack overflow? This seems a big limitation, how can be solved? Thank you in advance.

4
  • 4
    The contents of a std::vector don't live on the stack. Commented Feb 23, 2014 at 17:58
  • I don't see how .NET or Java tags are relevant here. Commented Feb 23, 2014 at 17:58
  • Thanks for formatting; that's much better! Commented Feb 23, 2014 at 17:59
  • Dynamic arrays dont go stack. Commented Feb 23, 2014 at 17:59

5 Answers 5

3

Large objects should have either static or dynamic storage duration.

Static:

int a[1000000];

void f()
{
    a[3] = 12;    // fine
}

Beware of shared, concurrent accesses to the static memory, though.

Dynamic (but managed properly by a suitable class):

void f()
{
    std::vector<int> a(1000000);   // dynamic objects managed by std::vector
    a[3] = 12;
}

Here each function call will create and manage its own dynamic allocation (and the complexities of concurrency are delegated to the memory allocator, so you don't have to think about those).

Sign up to request clarification or add additional context in comments.

Comments

2

There is no problem here.

This mean that if I create a std::vector and I push_back 600 000 times it goes in stack overflow? This seems a big limitation

No, because vectors elements do not have automatic storage duration (they don't "live on the stack"). They couldn't.

how can be solved

There is nothing to solve. Vector elements are dynamically allocated.

11 Comments

I'd like to use "double grid [15000][40]" not a vector, how can I do?
@user3343783: If you want that on the stack: (a) no you don't (b) you shouldn't (c) you can't (d) don't. You can get around it with a static object, but I still wouldn't.
So use vector<vector<double>> grid; and grid.resize(15000);, then for(i = 0; i < 15000; i++) grid[i].resize(4);.
@LightnessRacesinOrbit You should have taken that all the way to z ;)
@user3343783 - If you look at the implementation for std::array, you should see that one of the data members is a "regular" array. So yes, you get the same situation with std::array as you did with your original attempt.
|
0

You can define this array as

static double grid [15000][40];

As for std::vector then it allocates memory for its elements in the heap not in the stack.

Comments

0

memory of vector is heap allocated, you don't have to worry about the stack. Instead of doing push_back, you can use the member function resize :

 std::vector<int> grid;
 grid.resize(15000*40);

or even better, you can use a unique pointer if the grid has a fixed size

Comments

0

If you don't want to use std::vector for whatever reason, an alternative solution is:

int main()
{
  int (*grid)[40] = new int[15000][40];

  // work with grid
  grid[0][0] = 0.0;
  grid[14999][39] = 42;

  delete [] grid;
}

The usual caveats apply about raw pointers to dynamic storage causing memory leaks if the scope is terminated by an exception, so that the delete never executes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.