Solved

How to get stack size programatically

Posted on 1997-06-16
3
2,899 Views
Last Modified: 2013-12-03
How do get the size of the stack of my application from program code.  Unless a different value is specified via the /STACK statement on the command line of the linker, or the STACK statement in a program's .DEF file, the stack size of a Windows program is 1 megabyte.  How do I get this value from inside of the C++ program, short of reading the PE header.
0
Comment
Question by:Shrif
  • 2
3 Comments
 
LVL 3

Expert Comment

by:byang
ID: 1398425
Why do you need to know this ?
Well, I can think of one way to do this: get a pointer to a local variable, then move this pointer downward (ptr--) and use IsBadReadPtr() on it. Eventually you'll hit the stack guard page and IsBadReadPtr() will return non-zero. This will give you the bottom of the stack. Use the same approach to get the top address of the stack.
Of course, you can add/substract page size to the pointer for better performance.
I don't know if this has the effect of commiting physical memory to your entire stack range.
0
 

Author Comment

by:Shrif
ID: 1398426
I am sorry but, I really think there's a better answer.  This information should be available somewhere in the system.In answer to your question, I need to know this so that I can check for stack-overflows in recursive calls.  I have a built-in scripting engine in my program.  This software is maintained by a number of people so I really don't want to hard code 1-meg in my code.  Someone later may change the stack to be smaller for for whatever reason, and not change my hard coded number in C++
0
 
LVL 3

Accepted Solution

by:
byang earned 20 total points
ID: 1398427
OK, let's go assembly:

  DWORD dwStackSize; //size of current thread's stack
  _asm {
       mov eax,fs:[4]
       sub eax,fs:[8]
       mov dwStackSize,eax
  }

Don't know how to do this on non-intel CPU's.  
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

777 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