Solved

How to get stack size programatically

Posted on 1997-06-16
3
2,949 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

707 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