Jump to content
  • Advertisement
Sign in to follow this  
JohnsonGPS

Stack Overflow in Visual C++

This topic is 3864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!