We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How can you find out how much stack memory is available on your system?

tamiray
tamiray asked
on
Medium Priority
322 Views
Last Modified: 2008-02-01
I am a beginning C++ student and would like to write some code to return how much stack memory is available.  However, I can't find any functions to return available stack memory.  Any help is greatly appreciated.
Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
The amount of stack available is specified at compile/link time and can be different for each process. There usually are no functions provided to determine that amount.

Author

Commented:
Well, my instructor seems to think there is a way to determine this.  I'm wondering if there is something in the stdio.h library since I can return memory addresses using the & with my variables.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Are you sure it is about the stack? There are a few very system specific ways to determine the stack limit for a process, but no generic one.

Author

Commented:
The question is:

Discuss how you can find out how much stack memory is available on your system.

I found this on another site:
(If you are using Microsoft Visual C++, the stackavail function returns the amount of available stack space)

However, I can't find any information on stackavail.

Commented:
You could run a program like this and see what happens. It will eventually crash and the last line on the screen will show how much stack is at least available.

void endlessRecursion( char *last )
{
    static unsigned int size = 0;
    char mark;
    if( last )
        size += abs( last - &mark );
    printf( "now using %ld bytes of stack\n", size );
    endlessRecursion( &mark );
}

int main( int argc, char ** argv )
{
     endlessRecursion( 0 );
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
x4u is right, yet that would determine that value by creating a stack overflow error. On Windows, you can read that from the file header of an executable file, that's why I mentioned that this is quite system specific.
Couldnt you recurse until a stack overflow exception gets thrown? Then note the stack height and unwind.

Paul

Author

Commented:
You make it sound so easy!  Did I say that I am a BEGINNING C++ student?  Won't the stack overflow exception kill the program?  Is there a way to look for the exception and prevent the program from crashing?
elq

Commented:
If you're running on a *nix machine, you might want to look at
pthread_attr_getstacksize
elq

Commented:
Here's an example -

#include <stdio.h>

#include <pthread.h>

int main(void) {

    pthread_attr_t attrs;
    pthread_attr_init(&attrs);

    size_t size;
    pthread_attr_getstacksize(&attrs, &size);
    printf("size == %d\n\n", size);
}

on OSX -
size == 524288

on linux 2.6.15.x -
size == 10485760
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.