• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

MSVC++ stack overflow weird error

ElDonAntonio

Senior member
My big bro just asked me to help him port a program he did as part of his PhD thesis from SGI to Windows.

I got it to compile fine with minimal changes, but I get a stack overflow error right at the beginning of a function being called from main.

I tried tracing, and the stack overflow occurs right at the opening bracket of the function. I turned on dissassembly, and here's what I saw:

void function (blablabla)
710: {
0043AF80 push ebp
0043AF81 mov ebp,esp
0043AF83 mov eax,offset MESH_3D_ALTERED+0ABF70h (00532068)
0043AF88 call $$$00001 (004271b0) <=== STACK OVERFLOW
0043AF8D push ebx
0043AF8E push esi
0043AF8F push edi
0043AF90 lea edi,[ebp-532068h]
0043AF96 mov ecx,14C81Ah
0043AF9B mov eax,0CCCCCCCCh
0043AFA0 rep stos dword ptr [edi]

[C++ CODE]
...

From what I understand, the function tries to allocate too much memory from the stack. I looked in to the function, and if I remove a 5-dim static array of about 600 000 double values, the error goes away (so does the program's usefulness though).

It's the first time I encounter such a weird error, what's going on, and how do I solve that?

Thanks guys!
 
You need to increase stack size. I believe there is a flag to the linker to do that, however, I don't know exactly which one. Tried searching MSDN.
 
stack size is fixed for a program, typically only 1 MB (it's a Project > settings setting). Either change the stack size or change the stack allocations to a mallocs (don't forget the free() !)

void twiddle( ... )
{
double foo[ 60000 ] ;
}

void twiddle2( .... )
{
double* foo = (double* ) malloc( 60000 * sizeof (double) ) ;

....

free( foo ) ;
}

repeat as necessary for any other big allocations you find.
 
Thanks Dave! I tried putting the stack size to 50MB in the settings, but no change, same error.

A pretty basic question here, if I have a 5 dim static array like:

double myarray[11][11][9][24][24]

can I just change it to:

double *myarray = (double *)malloc(11*11*9*24*24*sizeof(double))

and keep the rest of the code as it is (indexing with the five indexes)???

I guess not, then I have to declare:

double *****myarray

and how do I allocate that to match my previous static array and keep the code as it is?

thanks!
 
N-dim arrays are always a pain!

Even with 2-dim foo[x][y] you have to define foo as double** then (a) malloc (x* sizeof( double* )) (b) loop x times to malloc a set of (y* sizeof(double) ) buffers. bleagh.

I suppose you could define a struct holding 4 of the dims, then malloc an array of the structs. ugh.

This cries out for some C++ standard library array class, but I haven't used those -- perhaps some STL guru can step in and learn ya 🙂
 
You don't want to deal with n-dim arrays. Instead "map" the n dimensions into a single one. That will be the easiest way. Wherever [x][y][z][w][v] is used, just replace with a function call. For example,

GetMappedArrayValue(ArrayRef, x, y, z, w, v)

It should be quite simple.
 
By the way, it is a TERRIBLE idea to allocate that much space on the stack. For a quick fix, make the array a global (static) variable.
 
Thanks singh, that's one heck of a simple and good idea to declare it as a global variable...

I already changed everything to dynamic allocations however, but I think there's a lot of similar cases on the way (I have two programs to port, the actual one being the simplest) and I might use the global variable idea instead.

It is indeed not a nice software design my bro got there, but he's in biomedical engineering so I can't really blame him 🙂

Once again, thanks a lot guys, you've helped me a good number of times these past days and I really appreciate it!
 
The best advice would be to allocate a 1 dimensional dynamic array and then just change the way you access it. Instead of array[a] use a[a * b + b]
 
Originally posted by: Argo
The best advice would be to allocate a 1 dimensional dynamic array and then just change the way you access it. Instead of array[a][ b ] use a[a * b + b]

That gets kind of ugly for 5-dim arrays 🙂


 
Back
Top