Operating System - HP-UX
1827697 Members
3201 Online
109967 Solutions
New Discussion

Treatment of Stack Space used for Function Calls

 
SOLVED
Go to solution
RW-S
Advisor

Treatment of Stack Space used for Function Calls

Hi,
Compiler : aCC
Platform : IA
OS : 11iv3

Doubt :
My question is over the stack space returned by a function after the function returns.

When a function call returns, what happens to the stack space? Is it cleaned (zeroed out)?
When a new function call happens in the same context is the same space re-used for that function or a new block of memory (but not the one used in the previously returned function) is allocated? Is there a way by means of flag to tell the Compiler to erase and then allocate.
Is this behavior of Stack Space Compiler dependent? or OS Dependent?

Has it anything to do with the Linker/Loader? (I don't think so tough ...)

Thanks in advance.
4 REPLIES 4
RW-S
Advisor

Re: Treatment of Stack Space used for Function Calls

To make the problem even more clear -

main() {
char *a[2];
foo (a);
bar (a);
}

foo() {
char c[20], d[20];
a[0]=c;
a[1]=d;
}

bar() {
printf(a[0],a[1]);
}

Here, even tough the arrays a and b have been destroyed after the function call of foo() has been done with, they still show the same value when the value of a is printed in the bar() function.
Dennis Handly
Acclaimed Contributor
Solution

Re: Treatment of Stack Space used for Function Calls

>what happens to the stack space?

It stays until overwritten. This could be randomly due to signals.

>is the same space re-used for that function

That's why it's called a stack, it is reused.

>Is there a way by means of flag to tell the compiler to erase and then allocate.

No. You must initialize your own objects.

>even though the arrays a and b have been destroyed

(Only C++ objects are destroyed.)

>they still show the same value when the value of a is printed in the bar() function.

This is illegal, you shouldn't depend on this.
Dennis Handly
Acclaimed Contributor

Re: Treatment of Stack Space used for Function Calls

If you are happy with the answers you were given, please read the following about how to assign points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33
T. M. Louah
Esteemed Contributor

Re: Treatment of Stack Space used for Function Calls

stack space reduction is a chanllenge and devellopers work hard to reduce number of elements pushed onto a program's stack space during program execution... there are tools/commpilers to help with code optimization.
t#
Little learning is dangerous!