finding stack location in C using program

Is there a way to find the address of stack memory writing a program? Please guide me

If the general area will do, you can just take the address of a local variable.

If you want the stack pointer itself, you can get that through assembly language.

If you need the top of it, on my system at least, it counts down from one system page less than the highest address.

Depending on your system, you might be able to see the memory arrangement by processing /proc/<processid>/maps.

What i am doing is the following...

#include<stdio.h>
//#include<conio.h>

Debuggers like gdb (assuming you are on Linux) will show you all of the stack in either a core dump or a running program.

If you are on Linux, the source code for va_start() and va_arg() (part of stdarg.h) will show you how to walk the current stack frame. The same is true for other compilers that are open source. Note that each implmentation of this is specific to the hardware it runs on. There is no "general" solution.

/proc/<pid>/maps is very much your friend as Corona says...

NO!

In some configurations, the stack won't overflow, it will just grow indefinitely, which case an infinite recursion will bring down the entire system instead of crashing.

On some compilers, an infinite recursion will be optimized into an infinite loop, which will just sit there and do nothing forever.

In general, one never tries to make his own program crash on purpose. That's for actual errors.

To get the maximum stack size, see 'man getrlimit'.

To get the starting stack position, if you are on linux, take a look at /proc/<pid>/maps like I already suggested.

Hi,

But in my case, I am getting a list of around a couple of thousand or nore addresses, and then an eventual segmentation fault. I am running my program on linux. Isnt this a possible way? I am not allowed to use any system command, and am supposed to report my findings obtained empirically. Please comment...

It is a possible way. It's also a bad way, for reasons I'm not going to repeat.

Better throw out printf, then.

Is this homework?

.................................................

Please read the unix.com rules, which state that homework problems are not allowed.

i am sorry sir, i did not know it..will delete all my scraps

how do you exactly differentiate between bringing down the system and crashing the system ?

crashing -- not working the way it is expected to work
bringing down - an essential reboot

(This question is out of curiosity and if my question is off-the topic kindly ignore my question ) :stuck_out_tongue: :stuck_out_tongue:

A crash is when that particular program dies.

Bringing down the system is when the fault brings down the entire system, in this case by allocating infinite amounts of memory until it's all used up and the system grinds to a halt.

Some, particularly older, kernels also go into a "panic". In that state the system is completely unusable. The kernel may also initiate shutdown. Or crash and dump core.