Jump to content
  • Advertisement
Sign in to follow this  
Zefrieg

Stack overflows and such

This topic is 5413 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

Using the new operator in C++, why do I get stack overflow errors in Visual C++ .NET when I allocate too large of a peice of memory like this: int* array = new int[1000000]; Shouldn't it be something like a heap overflow? Also, doing this in the linker: /STACK:100000000 increases the stack size, but now I have more heap? Does this deal with the way Windows handles memory for programs in that it gives each one 1mb or so? Is there something that says you can't allocate a chunk of memory in the heap that is larger than the stack size? I've been looking for articles on the net for why this is, and I can't find any.

Share this post


Link to post
Share on other sites
Advertisement
Also, what is better?

1. Increasing the stack size.

2. Dynamically increasing the stack size within the program by catching the exception and using some windows functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zefrieg
Using the new operator in C++, why do I get stack overflow errors in Visual C++ .NET when I allocate too large of a peice of memory like this:

int* array = new int[1000000];


It is not likely that new is causing a stack overflow since the two are unrelated. Here are some more likely causes:

1. Perhaps, an exception is being thrown and that is causing the stack overflow somehow.
2. Perhaps you have overridden new and that code is causing the stack overflow.
3. Perhaps the cause of the stack overflow is due to other code.
4. Perhaps your platform does not have virtual memory and the allocation is overflowing into the stack area.

Share this post


Link to post
Share on other sites
Ok, I figured out exactly what is occuring. It seems this only happens in debug mode. The problem, the memory IS being allocated on the stack. There is a special debug version of new that is called when in debug mode. Instead of allocating the memory on the heap, it allocates the memory on the stack! It does this because the debugger cannot step through heap memory.

Share this post


Link to post
Share on other sites
Umm, no. The debug version of new does not allocate on the stack. It allocates on the heap. It CAN walk the heap as well. Which suggests that either you've overridden new, or some library you are using has.

Share this post


Link to post
Share on other sites
Really? Well, that looks like a stack allocation to me.


0041DAD0 push ebp
0041DAD1 mov ebp,esp
0041DAD3 push 0FFFFFFFFh
0041DAD5 push offset __ehhandler$_main (450A4Ah)
0041DADA mov eax,dword ptr fs:[00000000h]
0041DAE0 push eax
0041DAE1 mov dword ptr fs:[0],esp
0041DAE8 mov eax,17D78554h
0041DAED call @ILT+1320(__alloca_probe) (41B52Dh)
0041DAF2 push ebx
0041DAF3 push esi
0041DAF4 push edi
0041DAF5 push offset Node<Vert,float>::~Node<Vert,float> (41B0BEh)
0041DAFA push offset Node<Vert,float>::Node<Vert,float> (41B41Ah)
0041DAFF push 989681h
0041DB04 push 28h

Share this post


Link to post
Share on other sites
Weird, it doesn't do this on other programs. Maybe something just messed up. Seems to use new when I copied the code to a new project.

Share this post


Link to post
Share on other sites
Yes I see, that is doing an alloca which is a stack alloc. This is not normal behavious. As Washu already pointed out, some code you are using is overriding new (and probably delete).

Maybe you could step into the new operator in the debugger and see where it goes?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!