Stack Overflow in Visual C++

Started by
10 comments, last by Zahlman 16 years, 5 months ago
I created a C++ program and defined a huge struct. The struct size is about 10000*100 bytes. I compiled the program in Borland C++ Builder6 and everything is OK. However, Visual C++.NET 2003 reported error as "Unhandled exception at ... Stack overflow". Visual C++ will not report any problem if I reduce the struct size to a much smaller one. Anybody know how to make VC++ accept huge struct and not cause stack overflow? Thank you in advance. My Struct Sample: typedef struct { double dVarXXX[10000]; }StructYYY; typedef struct { StructYYY myStructYYY[100]; }StructZZZ;
Advertisement
Allocate the object on the heap?

There may be a way to increase the default stack size, which is in the region of a few megabytes I think.
I don't have visual studio atm, but i remember increasing the "stack reserved size" to something like 100mb once. The option must be in Properties -> Configuration Properties -> Linker -> System -> Stack Reserve Size. However, i would also strongly recommend that you just allocate dynamically.
Quote:Original post by JohnsonGPS
I created a C++ program and defined a huge struct. The struct size is about 10000*100 bytes.


Don't do that on the stack. Big memory = put on heap.

Unless you have a very very good reason for it to be on the stack

-me
Yea, use the heap.

10000 * 100 * (8 bytes) = 7.62939453 megabytes
It's not impossible that the Borland compiler doesn't do error checking, and compiles, and even executes, but accessing later elemnts would corrupt the memory or cause a system exception.

Default stack in VS is 1 megabyte.
Quote:
I created a C++ program and defined a huge struct. The struct size is about 10000*100 bytes.

This is probably a Bad Idea. Why are you doing this? If it's for any reason other than exploring the stack size limitations and related stress points, there's probably a better way. Something that large should definately be dynamically allocated on the heap, at least.
Allocate the structure dynamically (using new and delete) solved my problem. Thank you!


Quote:Original post by D_Tr
I don't have visual studio atm, but i remember increasing the "stack reserved size" to something like 100mb once. The option must be in Properties -> Configuration Properties -> Linker -> System -> Stack Reserve Size. However, i would also strongly recommend that you just allocate dynamically.


Original post by JohnsonGPS
Allocate the structure dynamically (using new and delete) solved my problem. Thank you!
Or more likely, it allowed you to avoid the immediate problem.

As jpetrie asked, why on Earth do you need such a huge struct? Most likely that in itself is a symptom of another deeper design problem that you might as well get sorted out now.
Quote:Original post by JohnsonGPS
Anybody know how to make VC++ accept huge struct and not cause stack overflow?


There is an option somewhere (I believe it is a linker option) that lets you specify the stack size and/or increment. I don't remember exactly but I believe that VC++ gives you an "infinite" stack, but places a limit on the amount it can grow by in one shot (1 MB by default?).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement