I have a doubt regarding stack in c functions... Here is a program which shows an example...

#include<stdio.h>
int display();
main()
{
	int m=display();
	printf("m is : %d\n",m);
}
int display()
{
	printf("1234\n");
	return;
}

outputs:
1234
m is : 5

#include<stdio.h>
int display();
main()
{
	int m=display();
	printf("m is : %d\n",m);
}
int display()
{
	printf("123");
	return;
}

outputs
123
m is : 3

Can we not see the contents in stack? If yes how? Please reply me quickly...

what stack?

When a function is called the contents of it such as temporary variables used in that function, return variable, formal parameters etc will be stored in a stack.Can we not display that stack, it's way of arranging memory etc???

what stack?

When a function is called the contents of it such as temporary variables used in that function, return variable, formal parameters etc will be stored in a stack.Can we not display that stack? it's way of arranging memory etc???

I am pretty sure you are using the wrong term.

In C, if a function's return value is expected but not properly returned, the register that normally contains the return value will contain an undefined value which is basically whatever the register has in it before it left the function. I would consider this value "random" since its highly compiler specific.

In other words, in your compiler "m is: 3" but in another one it can say "m is: 0". Even compiler optimization settings can interfere with this result.

Also, what stack? This example has nothing to do with stacks. Maybe if you were passing parameters to "display()" that would make more sense.

Finally, what is your point here? What are you trying to accomplish? If you simply want to have more control of the stack and registers, consider using assembly language. If you just want to know how your code gets compiled, tell the compiler to generate an assembly code dump or just disassemble your binary.

When a function is called the contents of it such as temporary variables used in that function, return variable, formal parameters etc will be stored in a stack.Can we not display that stack? it's way of arranging memory etc???

No it can't be done. Anyway I can't think of a situation when someone would want to do that.

No it can't be done. Anyway I can't think of a situation when someone would want to do that.

Anything is possible in C, including viewing and working with the stack, but you would have to either write a wrapper in assembly to conform to the target platform or use compiler extensions that exposes that kind of low level access.

The only standard and legal use of working with stacks is with variadic functions using helper macros found in stdarg.h

Displaying the stack completely is complicated but anyway, it is possible to display the call stack completely. If you are interested in that go through this link http://www.linuxjournal.com/article/6391

I also forgot to mention that debuggers expose all CPU and stack information during runtime of the application.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.