finding stack location in C using program


 
Thread Tools Search this Thread
Top Forums Programming finding stack location in C using program
# 1  
Old 10-20-2006
finding stack location in C using program

Is there a way to find the address of stack memory writing a program? Please guide me
# 2  
Old 10-21-2006
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.
# 3  
Old 10-21-2006
What i am doing is the following...

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

Last edited by jacques83; 10-22-2006 at 01:07 AM..
# 4  
Old 10-21-2006
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...
# 5  
Old 10-21-2006
Quote:
Originally Posted by jacques83
By a recursive call, I am running till the stack overflows...Is this a good method to see the total stack, right from the 1st till the last address? Please comment....
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.
# 6  
Old 10-21-2006
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...
# 7  
Old 10-21-2006
Quote:
Originally Posted by jacques83
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?
It is a possible way. It's also a bad way, for reasons I'm not going to repeat.
Quote:
I am not allowed to use any system command
Better throw out printf, then.
Quote:
and am supposed to report my findings obtained empirically. Please comment...
Is this homework?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX Desktop Questions & Answers

Knowing the size and location of variables in a C program

So I need some help with this. Pardon me if I'm posting in the wrong forum, after some googling for my answer and finding nothing I found this forum. It seemed appropriate for what I was seeking. I just didnt find a forum that concerned the use of GDB. I'm learning to use the C language and GDB.... (2 Replies)
Discussion started by: Cambria
2 Replies

2. Programming

How to read max stack size -Xss that is set/default for a java program?

I need to know what is the maximum stack size i.e. -Xss my java program is running with. Is there a way to find that out from inside my java program code and outside of it. What i am looking for is to read whatever the current set max limit -Xss (stack sie) is for a particular JVM(not... (3 Replies)
Discussion started by: mohtashims
3 Replies

3. UNIX for Dummies Questions & Answers

program location

Hello all, one of application on system requires that "uname" program is in "/usr/uname" location. I can find uname in "/usr/bin/uname" location. Is it possible to present the /usr/bin/uname as that it was located in /usr/uname location? Thank you in advanced, M (1 Reply)
Discussion started by: kreno
1 Replies

4. Programming

Finding the path of the C program

Hi All, I have a c program called findPath.c in a path /home/harsh/c-Programs/. How can i find the path where the program is stored at runtime?? I have given the following #include<stdio.h> int main() { system("dirname $0"); return 0; } This is resulting in the output as . <single dot... (6 Replies)
Discussion started by: sreeharshasn
6 Replies

5. UNIX for Dummies Questions & Answers

finding my location in a string

I'm a relative newbie and apologize if this is silly... Suppose I have a file with English words and their Spanish translations. I read them in like this: while(getline < myfile > 0) { eng = $1 SPAN = $2 } Now, for every eng in SPAN, I want to scan through SPAN and search for a... (2 Replies)
Discussion started by: DrLeeDetroit
2 Replies

6. UNIX for Advanced & Expert Users

Finding register set being used in program

How can i find( or list) contents of all registers being used by my program? Is there any system call or library available for this?:confused: At runtime in my c/c++ program. At runtime using may be some assembly hack!!!!!!!!!!! (2 Replies)
Discussion started by: amit gangarade
2 Replies

7. Shell Programming and Scripting

Finding a word at specific location in a string

Hi All , I have different strings (SQL queries infact) of different lengths such as: 1. "SELECT XYZ FROM ABC WHERE ABC.DEF='123' " 2. "DELETE FROM ABC WHERE ABC.DEF='567'" 3. "SELECT * FROM ABC" I need to find out the word coming after the... (1 Reply)
Discussion started by: swapnil.nawale
1 Replies

8. Programming

access variable through program stack

I am working on garbage collector in C? How should :confused: I find the part of heap where the variable are stored. It there any compiler (GCC) support for this. (2 Replies)
Discussion started by: amit gangarade
2 Replies

9. UNIX for Dummies Questions & Answers

Finding files in subdirectories from one location

Using ssh, is there a simple command to find files in subdirectories from, let's say, the home directory on a shared server? (2 Replies)
Discussion started by: endl
2 Replies

10. Filesystems, Disks and Memory

get stack trace from C program on Solaris 8

I'm on solaris 8. I need to check the stack trace inside my C program. I don't have printstack or walkstack. I tested getcontext and it works. But how do I get the symbols from "stack_t" ? Help please. Many thanks! (4 Replies)
Discussion started by: rydahl
4 Replies
Login or Register to Ask a Question