Stack emptying

L

Lee

Is there any method by which we could empty the stack,(pop the function
call addresses and return addresses stored in it)? Can we atleast
partially free the stack during program run time??

Thanks
Lee
 
A

Alexei A. Frounze

Lee said:
Is there any method by which we could empty the stack,(pop the function
call addresses and return addresses stored in it)? Can we atleast
partially free the stack during program run time??

Not in the standard C. Try assembly for such hacks. In C the only way to
free up some stack is to return to the caller... Not sure if set/long jump
can help you. I'd advise just to keep minimum of variables on the stack and
better use malloc()/free() for big things. That should help unless you're in
deep recursion or too tight on stack. Maybe you should consider algorithm
replacement -- trade speed for memory.

HTH,
Alex
 
D

David Resnick

Lee said:
Is there any method by which we could empty the stack,(pop the function
call addresses and return addresses stored in it)? Can we atleast
partially free the stack during program run time??

Thanks
Lee

You can subvert the normal ordering of function calls/returns using
setjmp/longjmp, take a look and see if that does what you want.

If not, perhaps you should look at system specific extensions
that manipulate things as you wish or go to a different language
(say, C++ and exceptions).

-David
 
G

gene.ressler

In fact the internals of setjmp() generally save the entire processor
state including the stack pointer. Then longjmp() restores the saved
stack pointer value. This effectively chops the stack back to its
state just after setjump() was called. When longjmp()'s epilogue code
runs, the original setjmp() return address is popped off the stack and
execution continues.

So to chop the stack all the way back you'd want to setjmp() in main().
Then a later longjmp() will cut the stack back to its initial state.
 
R

robertwessel2

In fact the internals of setjmp() generally save the entire processor
state including the stack pointer. Then longjmp() restores the saved
stack pointer value.


Usually much less than the entire processor state. Usually something
approximating the set of application visible registers that are
callee-save or global in the implementation's ABI.
 
K

Keith Thompson

Lee said:
Is there any method by which we could empty the stack,(pop the function
call addresses and return addresses stored in it)? Can we atleast
partially free the stack during program run time??

I'm not even sure what that means. The C standard doesn't even
require the existence of a stack, except that local data in functions
is allocated and deallocated in a stack-like manner.

What exactly are you trying to do, and why? If you've popped return
addresses from the stack, what do you expect to be able to do next?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top