1

I want to create 2d datastructure of [300][300], but I am getting stack overflow error.

Here is how I have defined,

typedef struct DST
{
    float salary;
    float x1;
    float x2;
};

struct DST person[300][300];

I am getting an error for the above declaration.

but, if I declare struct DST person[300][70]; --> this is working fine

So, it is a memory handling issue, I am unable to solve it.

Software: Visual Studio 2010

Could anybody please help me out with this?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Prajwal_7
  • 109
  • 1
  • 16

5 Answers5

4

Structure DST contains 12B of data. When memory alignment is turned on (and set to its default value - 8B) it takes 16B for each instance.

300*300*16B = 1440000B = ~1.4MB

Local variable declared like this get allocated on the stack and since the default stack size for Visual Studio is 1MB, the error is appropriate.

To fix this you can either increase the stack size /F 2000000 (I don't think it's good practice to set up larger stack), or rather allocate memory on the heap using:

Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96
2

300 * 300 * 12 bytes makes just over 1MB. Which is a VERY large set to put on the stack.

The simple solution in C++ is to use std::vector.

std::vector<std::vector<DST>> person(300);
for(int i = 0; i < 300; i++) 
  person[i].resize(300); 

This avoids direct usage of new (and then needing to use delete later).

If you have a C++11 compiler, you could do:

std::vector<std::array<DST, 300>> person; 

and thus avoid the loop to resize.

Note that although it's possible to "ask for a bigger stack", that should not really be used to allocate single large data structures on the stack. Larger stack is NECESSARY when you have lots of call levels, each of which have small amounts of data on the stack. Anything much larger than 1KB should make you think "Should this really go on the stack, or should I do something else".

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I like this one better because containers! Not as clumsy as a pointer. An elegant weapon, for a more civilized age. – ftwl Jul 10 '14 at 07:09
  • I don`t like it because of `vector`. `vector` would be much better. @ftwl: I would call the `resize` loop clumsy. – nwp Jul 10 '14 at 07:12
  • @nwp: That REQUIRES C++11, but does get rid of the loop. – Mats Petersson Jul 10 '14 at 07:14
  • How about array ? Both dimensions are known. – Quentin Jul 10 '14 at 07:34
  • @quentin: That doesn't solve anything in the original question, as `std::array` is just a nice wrapper to produce a `[]` style array, with some inline functions to make it look similar to a `std::vector` and other containers (`std::array` has `size`, `begin`, `end` etc) – Mats Petersson Jul 10 '14 at 07:46
  • Definitely the correct structure when you have all the dimensions though. Throw in a `unique_ptr` to allocate it dynamically and you're good to go, no resizing to do, no fears about reallocation. – Quentin Jul 10 '14 at 07:59
0

Since you typedef the struct you don't need to use the struct keyword when initializing it, but you need to give it a name. It should look something like this:

typedef struct DST
{
    float salary;
    float x1;
    float x2;
} DST;

DST person[300][300];

Also since you're not allocating it using malloc it's being allocated on the stack, so as soon as you leave the function you declare it in you won't be able to access it anymore.

PandaConda
  • 3,396
  • 2
  • 20
  • 22
0

This is the question to ask on stackoverflow.

Together with the two solutions mentioned in other answers - allocating the array on heap or increasing stack size, there is a third one.

If you define the array static or in global scope, it will be created in the program data segment.

For simple proof of concept programs this may the simplest solution. Also, it may generate fastest code on some (old, simple) compilers.

In general, this is not good style.

Just cut and paste your definion from a funciton definition to file scope....

Jakub
  • 583
  • 3
  • 8
0

Thank You all. :)

To be honest, I am very poor at memory operations. Looking into various possible solutions I realized I am yet to learn a lot.

These answers have solved my issue.

Multiple possible solutions were suggested.

Currently I have used: using std::vector

std::vector<std::vector<DST>> person(300);
for(int i = 0; i < 300; i++) 
person[i].resize(300);  

This is working absolutely fine.

I will explore on all other solutions and work on it. Thank You

Prajwal_7
  • 109
  • 1
  • 16