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

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.
tamirayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
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.
0
tamirayAuthor 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.
0
jkrCommented:
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.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

0
x4uCommented:
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 );
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
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.
0
PaulCaswellCommented:
Couldnt you recurse until a stack overflow exception gets thrown? Then note the stack height and unwind.

Paul
0
tamirayAuthor 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?
0
elqCommented:
If you're running on a *nix machine, you might want to look at
pthread_attr_getstacksize
0
elqCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.