code to find the top of the stack, not able to figure it out


 
Thread Tools Search this Thread
Top Forums Programming code to find the top of the stack, not able to figure it out
# 1  
Old 04-21-2010
Error code to find the top of the stack, not able to figure it out

Code:
OFFSET=100;
PAGESIZE=4096;
int dummy_last;

 TOPSTACK = (caddr_t)(&dummy_last - OFFSET);
 TOPSTACK = (caddr_t)((unsigned long)TOPSTACK - 
                               ((unsigned long)TOPSTACK % PAGESIZE));

this i a code to find the top of the stack, but not able to figure it out. can anyone please help me out to what this code actually does?

Last edited by vbe; 04-21-2010 at 05:02 AM.. Reason: dont forget code tags... more suitable title...
# 2  
Old 04-21-2010
This will find the top of the page of memory the variable's in, assuming a system where pages are 4096 bytes(not always a good assumption!) but won't actually find the top of the stack unless you're within 4096 bytes of it. If you calculate it from inside main(), this is possible if you don't have too many local variables. If you're in linux, you can find the top of the stack more reliably by reading /proc/self/maps, which is a human-readable file listing memory segments for a process one segment per line. The one labelled [stack] is what you want.

Because memory's divided into chunks of fixed size, 4096 on many systems, the stack will always begin at a multiple of the pagesize. Your code works by dividing memory into 4096-byte chunks, which it is on many systems, and subtracting any remainder from it. Remainder works like 4097 % 4096 = 1, 8192 % 4096 = 0, 8193 % 4096 = 1 etc. so you can do pos-=(pos%4096) to rewind the number it to the beginning of a multiple of 4096.

You should use getpagesize() instead of 4096.

Why they're adding 100 to it after all that, I can only guess. They may be calculating the address of something inside the stack rather than the very top of the stack.

Last edited by Corona688; 04-21-2010 at 03:54 PM..
# 3  
Old 04-22-2010
thanks a lot corona688 for the suggestionSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with change significant figure to normal figure command

Hi, Below is my input file: Long list of significant figure 1.757E-4 7.51E-3 5.634E-5 . . . Desired output file: 0.0001757 0.00751 0.00005634 . . . (10 Replies)
Discussion started by: perl_beginner
10 Replies

2. Red Hat

How to find memory taken by a process using top command?

I wanted to know how to find the memory taken by a process using top command. The output of the top command is as follows as an example: Mem: 13333364k total, 13238904k used, 94460k free, 623640k buffers Swap: 25165816k total, 112k used, 25165704k free, 4572904k cached PID USER ... (6 Replies)
Discussion started by: RHCE
6 Replies

3. Shell Programming and Scripting

Can't figure out how to find specific characters in specific columns

I am trying to find a specific set of characters in a long file. I only want to find the characters in column 265 for 4 bytes. Is there a search for that? I tried cut but couldn't get it to work. Ex. I want to find '9999' in column 265 for 4 bytes. If it is in there, I want it to print... (12 Replies)
Discussion started by: Drenhead
12 Replies

4. Shell Programming and Scripting

How to find stack size at runtime?

hi, I just wondering is there any way that I can record the stack size of shell script at runtime. (2 Replies)
Discussion started by: yc962frank
2 Replies

5. UNIX for Dummies Questions & Answers

how to find top 3 users currently logged on

For the first 3 users only that are currently logged in output their effective user id. thank you. (6 Replies)
Discussion started by: whyatepies
6 Replies

6. Shell Programming and Scripting

How to exclude top level directory with find?

I'm using bash on cygwin/windows. I'm trying to use find and exclude the directory /cygdrive/c/System\ Volume\ Information. When I try to use the command below I get the error "rm: cannot remove `/cygdrive/c/System Volume Information': Is a directory. Can someone tell me what I am doing... (3 Replies)
Discussion started by: siegfried
3 Replies

7. AIX

How to find the top 6 users (which consume most space)?

Hi everybody, I want to know if there is any posibility to find out - on an AIX system - which are the the users who consume most space or at least a posibility to obtain a list with all the users and how much space are they consuming ? Trying to use du command was useless. Any idea?... (5 Replies)
Discussion started by: RebelDac
5 Replies

8. Programming

How to find out the stack size occupied for a process?

Hi, In Linux how to find out what will be the stack size allocated for a process? Actually i have to fork n number of processess, and will call exec. I will be execing an executable which is already multithreaded and each thread size is defined. My doubt is how to know if the size of the... (2 Replies)
Discussion started by: rvan
2 Replies
Login or Register to Ask a Question