?
Solved

How to get stack size programatically

Posted on 1997-06-16
3
Medium Priority
?
2,961 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 80 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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