request for stack advice

Justin M. Lee jlee@cs.utexas.edu
Mon Apr 26 17:00:00 GMT 2004


Folks,

I recently inherited some C that crashes when compiled with 3.2-3.3
compilers on Linux x86.  It works on SGI's compiler on Irix and gcc 2.95
on Linux x86.

This code is a bit strange - it puts huge arrays on the stack.  We're
talking about several megs.  In a debugger, it appears that the crash
happens when it tries to touch something on the stack in a particular
call.  I came to realize that I don't understand how stack allocation
works.  Looking at the assembly code, a function doesn't really request
more stack -- it just subtracts from esp.  How does the OS know to
allocate pages to the stack area?  I saw something about stack space in
the ld man page, but it didn't know what I was talking about when I tried
to pass it in via -Xlinker.  Is there a default stack space?

Some more info: it looks to me like the esp is valid at the point of the
crash: its about 8 megs less at the point of the crash than at main.  The
reason I even got here was because I thought the stack was getting
trashed.

At main:
esp 0xbfffe9d0
ebp 0xbffff458

At point of crash
esp 0xbf7ab948
ebp 0xbffe9268

One more thing: removing all this stuff from the stack and mallocing it
fixes the problem.  I'm just curious at this point.

I'm sure this is all either obvious or clearly documented somewhere, but I
couldn't google for it for the life of me.  Feel free to tell me to RTFM
(but please point me to the right chapter).  I only ask here because I'm
trying to figure out if there is something specific in the compiler
differences that could have caused this (and because someone here might
actually know the answers to my questions :) ).

--Justin



More information about the Gcc mailing list