Solved

How to get stack size programatically

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…

830 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