Solved

How to get stack size programatically

Posted on 1997-06-16
3
2,866 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now