?
Solved

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

Posted on 2006-03-25
10
Medium Priority
?
286 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.
0
Comment
Question by:tamiray
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16290191
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
 

Author Comment

by:tamiray
ID: 16291051
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
 
LVL 86

Expert Comment

by:jkr
ID: 16291095
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:tamiray
ID: 16291159
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
 
LVL 11

Accepted Solution

by:
x4u earned 2000 total points
ID: 16291477
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
 
LVL 86

Expert Comment

by:jkr
ID: 16291885
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16293473
Couldnt you recurse until a stack overflow exception gets thrown? Then note the stack height and unwind.

Paul
0
 

Author Comment

by:tamiray
ID: 16294114
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
 

Expert Comment

by:elq
ID: 16298266
If you're running on a *nix machine, you might want to look at
pthread_attr_getstacksize
0
 

Expert Comment

by:elq
ID: 16298305
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question