Sponsored Content
Top Forums Programming code to find the top of the stack, not able to figure it out Post 302415038 by Corona688 on Wednesday 21st of April 2010 02:45:58 PM
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..
 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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
DLAQP2(l)								 )								 DLAQP2(l)

NAME
DLAQP2 - compute a QR factorization with column pivoting of the block A(OFFSET+1:M,1:N) SYNOPSIS
SUBROUTINE DLAQP2( M, N, OFFSET, A, LDA, JPVT, TAU, VN1, VN2, WORK ) INTEGER LDA, M, N, OFFSET INTEGER JPVT( * ) DOUBLE PRECISION A( LDA, * ), TAU( * ), VN1( * ), VN2( * ), WORK( * ) PURPOSE
DLAQP2 computes a QR factorization with column pivoting of the block A(OFFSET+1:M,1:N). The block A(1:OFFSET,1:N) is accordingly pivoted, but not factorized. ARGUMENTS
M (input) INTEGER The number of rows of the matrix A. M >= 0. N (input) INTEGER The number of columns of the matrix A. N >= 0. OFFSET (input) INTEGER The number of rows of the matrix A that must be pivoted but no factorized. OFFSET >= 0. A (input/output) DOUBLE PRECISION array, dimension (LDA,N) On entry, the M-by-N matrix A. On exit, the upper triangle of block A(OFFSET+1:M,1:N) is the triangular factor obtained; the ele- ments in block A(OFFSET+1:M,1:N) below the diagonal, together with the array TAU, represent the orthogonal matrix Q as a product of elementary reflectors. Block A(1:OFFSET,1:N) has been accordingly pivoted, but no factorized. LDA (input) INTEGER The leading dimension of the array A. LDA >= max(1,M). JPVT (input/output) INTEGER array, dimension (N) On entry, if JPVT(i) .ne. 0, the i-th column of A is permuted to the front of A*P (a leading column); if JPVT(i) = 0, the i-th col- umn of A is a free column. On exit, if JPVT(i) = k, then the i-th column of A*P was the k-th column of A. TAU (output) DOUBLE PRECISION array, dimension (min(M,N)) The scalar factors of the elementary reflectors. VN1 (input/output) DOUBLE PRECISION array, dimension (N) The vector with the partial column norms. VN2 (input/output) DOUBLE PRECISION array, dimension (N) The vector with the exact column norms. WORK (workspace) DOUBLE PRECISION array, dimension (N) FURTHER DETAILS
Based on contributions by G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain X. Sun, Computer Science Dept., Duke University, USA LAPACK version 3.0 15 June 2000 DLAQP2(l)
All times are GMT -4. The time now is 01:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy