Can we determine stack size & Heap size at runtime ?

Discussion in 'C Programming' started by sunny, Aug 16, 2006.

  1. sunny

    sunny Guest

    Hi All

    Is there any way to determine stack and heap size, during runtime.
    i.e can we predict stack overflow. etc
     
    sunny, Aug 16, 2006
    #1
    1. Advertising

  2. sunny wrote:
    > Hi All
    >
    > Is there any way to determine stack and heap size, during runtime.
    > i.e can we predict stack overflow. etc


    IMHO this is nothing the C standard can address.

    You CAN on many (but not all) systems do a little math on pointer
    variables to figure out how much the stack has grown, something like:

    unsigned long int hi;

    void deeplynested(){ unsigned long int lo, len;
    lo = (unsigned int) &lo;
    len = hi - lo;
    printf("stack is about %d bytes\n", len );
    }

    int main ( .... ){ int mainvar; hi = (unsigned long int) &mainvar;
    .... call stuff... return 0;}

    .... as for figuring out heap usage, you could write a wrapper for
    malloc() that keeps track of the amount allocated so far. But this
    will not tally the overhead per block for the usual block headers, so
    it will be an undercount to some extent. And keeping track of free()ed
    memory gets a bit tricky. Plus its hard to link in your wrapper so all
    library routines go through the wrapper. Ugh.

    But it *can* be done and is very handy to find places where there's
    excessive stack or heap usage.
     
    Ancient_Hacker, Aug 16, 2006
    #2
    1. Advertising

  3. sunny

    MQ Guest

    sunny wrote:
    > Hi All
    >
    > Is there any way to determine stack and heap size, during runtime.
    > i.e can we predict stack overflow. etc


    On Linux, you can get/set heap size with the sbrk() function. Not too
    sure about stack size though. Linux probably has an initial stack size
    that is probably kernel-version dependant. If you overflow the stack,
    I think Linux will just make it bigger.

    I don't known anything about Windows. You'd probably get a better
    response on a forum for the platform you are developing

    MQ
     
    MQ, Aug 16, 2006
    #3
  4. In article <>,
    MQ <> wrote:

    >> Is there any way to determine stack and heap size, during runtime.
    >> i.e can we predict stack overflow. etc


    >On Linux, you can get/set heap size with the sbrk() function.


    To be overly picky, sbrk() does not allow you to get the heap
    size, sbrk() returns an address. You can only calculate the heap
    size if somehow you manage to sbrk() before any allocation is done;
    otherwise the best you can do is to find the size change.

    But [getting further offtopic] some of the Linux memory allocators
    work by using mmap() to allocate memory segments that are -not-
    in the heap; sbrk() cannot determine the size of those.

    >Not too
    >sure about stack size though. Linux probably has an initial stack size
    >that is probably kernel-version dependant.


    I don't know about Linux; in SGI IRIX, the stack position is determined
    by the linker (taking into account which ABI you are using).

    >You'd probably get a better
    >response on a forum for the platform you are developing


    Indeed, we've already gone too platform-dependant in this thread.

    --
    Programming is what happens while you're busy making other plans.
     
    Walter Roberson, Aug 16, 2006
    #4
  5. "sunny" <> wrote in message
    news:...
    > Is there any way to determine stack and heap size, during runtime.
    > i.e can we predict stack overflow. etc


    The C Standard does not require an implementation to have a stack or heap at
    all, so there is no portable answer to your question.

    Individual implementations may provide non-portable solutions; check in a
    newsgroup for the system(s) you care about.

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Stephen Sprunk, Aug 16, 2006
    #5
  6. sunny

    CBFalconer Guest

    Stephen Sprunk wrote:
    > "sunny" <> wrote in message
    >
    >> Is there any way to determine stack and heap size, during runtime.
    >> i.e can we predict stack overflow. etc

    >
    > The C Standard does not require an implementation to have a stack
    > or heap at all, so there is no portable answer to your question.
    >
    > Individual implementations may provide non-portable solutions;
    > check in a newsgroup for the system(s) you care about.


    Well, at least one out of four responses was accurate and topical.

    --
    "I support the Red Sox and any team that beats the Yankees"
    "Any baby snookums can be a Yankee fan, it takes real moral
    fiber to be a Red Sox fan" - "I listened to Toronto come back
    from 3:0 in '42, I watched Boston come back from 3:0 in '04"
     
    CBFalconer, Aug 17, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ivan
    Replies:
    4
    Views:
    2,070
    Michael Wojcik
    Oct 22, 2005
  2. Pep
    Replies:
    10
    Views:
    646
    Greg Comeau
    Aug 17, 2006
  3. Michal Slocinski

    Heap dump file size vs heap size

    Michal Slocinski, Mar 25, 2008, in forum: Java
    Replies:
    1
    Views:
    751
    GArlington
    Mar 25, 2008
  4. amit
    Replies:
    1
    Views:
    971
    Eric Sosman
    Dec 5, 2009
  5. Raymond Schanks
    Replies:
    0
    Views:
    557
    Raymond Schanks
    Apr 11, 2010
Loading...

Share This Page