Vista Crashing Static Memory Allocation?

I am using Microsoft Visual C ++ 2008 Express and have a rather annoying problem. It doesn't seem to be happening in XP, but in Vista I can't seem to find a way around it. Whenever I declare variables non-dynamically, if their total size exceeds about 30MB, the program immediately fires on startup. I know Vista limits non-Win32 applications to 32MB of memory, but I don't think that's my problem. I am using the Microsoft compiler and this happens regardless of whether it is a win32 console application or a win32 windowed application. I am simply stating that ...

int foo[1000][1000]

      

... or any combination of variables resulting in the same size anywhere, and that's a pretty app. It's funny that about 25% of the time it works, although this error exists. Am I missing some fundamental program here? Deprecated static allocation? Do I have to redo my entire application to use dynamic placement?

0


source to share


3 answers


Obsolete static allocation?

You are not doing static allocation - you are doing automatic allocation, and as others have said, you are running out of stack.

There are three main ways to reserve space for data in C ++:



  • On the stack, they are called "automatic variables", and they are normal local variables. Assuming your "int foo [] []" is local to main (), then this is what it is. Automatic data is limited by the available stack size, but is allocated very quickly (mostly zero time).

  • Statically they are either function-local variables or class variables that are executed by the word "static", or they are variables defined by external functions or class scope. Static data is reserved by the compiler. There is no backup time overhead, but memory is reserved for the entire runtime of the application.

  • On the heap - they are allocated "new" or "malloc" or some mechanism that makes these calls internally. Allocation and release are slow compared to the first two, but you can have as much memory as the system will give you, and you can reclaim it when you're done with it.

There are subtle variations on these three - alloca, for example, is a hybrid of 1 and 3, but these are the basics.

+3


source


The stack size can be set to be set, which defaults to something small. It's been a long time since I needed to play with these settings.

In the most likely links

I only have MSDEV 2005 at work, but here's what it says about the stack linker option:

The / STACK option sets the stack size in bytes. This parameter is used only when creating an .exe file.



This option determines the overall stack allocation in virtual memory. The default stack size is 1 MB. The linker rounds the specified value to the nearest 4 bytes.

EDIT

Unless you're doing your own memory management, I don't understand why you are putting it statically. But even so, I would dynamically allocate memory in front ...

+1


source


The problem is that non-dynamically allocated variables in methods are allocated on the stack, and the maximum stack size is MUCH less than the total available memory. Yes, that's roughly 30MB on Windows. What you've done here is, ironically, the site's namesake. Stack overflow.

Edit: According to http://www.cs.nyu.edu/exact/core/doc/stackOverflow.txt the max window stack size is 32MB.

0


source







All Articles