stack overflow

Hi,

I have a .txt file I need to into an array for processing.

File size is 3000 lines and there are 13 info separated by a ";".

Now to insert the full file I have created an array of strings : string array[3000][13]

However I have a stack overflow issue as soon as I launch the program.

Is this normal ? Should I resort to something else ?

Thanks

Wissam
Last edited on
Without your code, it's hard to tell.
wboustany wrote:
Is this normal ?

Yes. The stack isn't big enough to hold all that data.

wboustany wrote:
Should I resort to something else ?

Yes. The solution here is dynamic memory allocation.
I believe using a vector of records is a good approach:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <vector>

//...

struct Record
{
    int x;
    int y;
    int z;

    int other_data[10];
};

//...

std::vector<Record> my_data;
my_data.reserve(3000);

Record current_record;

while ( /* there are records in file */ )
{
    // read current record from file

    my_data.push_back(current_record);
}

//... 

Useful links:

http://cplusplus.com/doc/tutorial/structures/
http://cplusplus.com/reference/stl/vector/
Thx m4ster r0shi,

I managed to not use vectors (solely because I am lazy) but now I realise I will have to dig in them after all...will check the docs.

How viable would it be to simply increase the size of the stack and how would I do that for a two dimensional array of strings ?

Thanks again.
I don't know if/how you can increase the stack size programmatically. I wouldn't do that.
If you don't want to use vectors and structs, you can always use plain pointers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>

//...

std::string ** array;

array = new std::string * [3000];

for (int i = 0; i < 3000; i++)
    array[i] = new std::string[13];

// use your array here

for (int i = 0; i < 3000; i++)
    delete[] array[i];

delete[] array;

//... 

Useful link -> http://cplusplus.com/doc/tutorial/dynamic/
Last edited on
closed account (zb0S216C)
The MSDN website has a webpage on stack sizes[1]. It refers to the modification of a stack size in VCE2010, along with other versions of VCE.

References:
[1]http://msdn.microsoft.com/en-us/library/8cxs58a6(v=VS.100).aspx

Wazzak
Last edited on
Thank you both very much for your answers. Will try both solutions tomorrow.

Regards,

Wissam
If I may have one last question on this topic...

When resorting to dynamic arrays, does that not eventually lead t the same problem ? (stack overflow)

I mean, at the end the "dynamic array" will have to be as big as the static array no ?
wboustany wrote:
I mean, at the end the "dynamic array" will have to be as big as the static array no ?

Correct. But a dynamic array isn't stored on the stack,
it's stored on the heap ;) So are the elements of a vector.

If you google stack vs heap you'll find many useful links.
Topic archived. No new replies allowed.