The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Heap and stack naan High Level Programming 5 04-29-2008 11:40 PM
what is stack winding and stack unwinding amitpansuria High Level Programming 2 08-07-2007 02:46 PM
get stack trace from C program on Solaris 8 rydahl Filesystems, Disks and Memory 4 03-23-2007 10:53 AM
stack region yakari UNIX for Advanced & Expert Users 2 12-29-2006 06:34 AM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 01:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 10-20-2006
Registered User
 

Join Date: Sep 2006
Posts: 19
finding stack location in C using program

Is there a way to find the address of stack memory writing a program? Please guide me
Reply With Quote
Forum Sponsor
  #2  
Old 10-20-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
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.
Reply With Quote
  #3  
Old 10-20-2006
Registered User
 

Join Date: Sep 2006
Posts: 19
What i am doing is the following...

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

Last edited by jacques83; 10-21-2006 at 09:07 PM.
Reply With Quote
  #4  
Old 10-21-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
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...
Reply With Quote
  #5  
Old 10-21-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
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.
Reply With Quote
  #6  
Old 10-21-2006
Registered User
 

Join Date: Sep 2006
Posts: 19
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...
Reply With Quote
  #7  
Old 10-21-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
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?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0