Vista中的静态内存分配导致崩溃?

时间:2021-10-28 13:16:39

I'm using Microsoft Visual C++ 2008 Express, and have a pretty annoying problem. It doesn't seem to happen in XP but in Vista I can't find a way around it. Whenever I declare variables non-dynamically, if their combined size exceeds about 30mb, the program will crash immediately at start-up. I know that Vista limits non-Win32 apps to 32mb of memory, but I don't think that's my issue. I'm using the Microsoft compiler, and it happens regardless if it's a win32 console app or a win32 window app. I just declare like...

我正在使用Microsoft Visual C ++ 2008 Express,并且有一个非常讨厌的问题。它似乎不会发生在XP中,但在Vista中我无法找到解决方法。每当我非动态地声明变量时,如果它们的组合大小超过大约30mb,程序将在启动时立即崩溃。我知道Vista将非Win32应用程序限制为32mb内存,但我不认为这是我的问题。我正在使用Microsoft编译器,无论它是win32控制台应用程序还是win32窗口应用程序,它都会发生。我只是声明......

int foo[1000][1000]

...or any combination of variables resulting in a similar size anywhere, and that's good-bye-application. Funny thing is, about 25 % of the times it runs even though this error exists. Am I missing some fundamental programming thingy here? Is static allocation obsolete? Am I going to have to redo the entire application to make use of dynamic allocation?

...或任何变量的组合,导致任何地方都有类似的大小,那就是再见 - 应用程序。有趣的是,即使存在此错误,它仍会运行约25%的次数。我在这里错过了一些基本的编程吗?静态分配是否已过时?我是否必须重做整个应用程序才能使用动态分配?

3 个解决方案

#1


3  

Is static allocation obsolete?

静态分配是否已过时?

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

你没有进行静态分配 - 你正在进行自动分配,正如其他人所说的那样,你正在耗尽堆栈。

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

在C ++中基本上有三种为数据保留空间的常用方法:

  1. On the stack - these are called 'automatic variables', and they're what ordinary function-local variables are. Assuming your "int foo[][]" is local to main(), then this is what this is. Automatic data is limited by the available stack size, but it's very fast to allocate (basically zero time).

    在堆栈上 - 这些被称为“自动变量”,它们是普通的函数局部变量。假设你的“int foo [] []”是main()的本地,那么这就是这个。自动数据受可用堆栈大小的限制,但分配速度非常快(基本上为零时间)。

  2. Statically - these are either function-local or class variables which are proceeded by the word 'static', or they're variables defined outside functions or classes scope. Static data is reserved by the compiler There's no allocation time overhead, but the memory is reserved for the entire run-time of the application.

    静态 - 这些是函数本地或类变量,由“静态”一词继续,或者它们是在函数或类范围之外定义的变量。静态数据由编译器保留。没有分配时间开销,但内存是为应用程序的整个运行时保留的。

  3. On the heap - these are allocated with 'new' or 'malloc' or some mechanism which makes those calls internally. Allocation and release are slow compared with the first two, but you can have as much memory as the system will give you, and you can return it when you've finished with it.

    在堆上 - 这些分配有'new'或'malloc'或一些在内部进行调用的机制。与前两个相比,分配和释放速度很慢,但是你可以拥有系统给你的那么多内存,你可以在完成它后返回它。

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

这三个有微妙的变化 - 例如,alloca是1和3的混合,但这些是基础。

#2


1  

There might be a stack size setting you need to set that is defaulting to something small. It has been a long time since I needed to play with those settings.

可能存在需要设置的堆栈大小设置,该默认设置为默认值。我需要玩这些设置已经有很长一段时间了。

In the link options most likely

在链接选项中最有可能

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

我只有MSDEV 2005正在工作,但这里是关于堆栈链接器选项的说法:

The /STACK option sets the size of the stack in bytes. This option is only for use when building an .exe file.

/ STACK选项以字节为单位设置堆栈的大小。此选项仅在构建.exe文件时使用。

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

此选项指定虚拟内存中的总堆栈分配。默认堆栈大小为1 MB。链接器将指定值向上舍入为最接近的4个字节。

EDIT

Unless you are doing your own memory management I can't see why you would allocate this statically. But even in that case I would dynamically allocate memory up front...

除非您正在进行自己的内存管理,否则我无法理解为什么要静态分配它。但即使在那种情况下,我也会动态分配内存...

#3


0  

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. I think it's around 30MB in Windows, yes. What you have done here is, ironically, this very site's namesake. A Stack Overflow.

问题是方法中的非动态分配变量是在堆栈上分配的,并且最大堆栈大小比总可用内存小很多。我认为它在Windows中约为30MB,是的。具有讽刺意味的是,你在这里所做的就是这个网站的名字。堆栈溢出。

Edit: According to http://www.cs.nyu.edu/exact/core/doc/*.txt ,Window's maximum stack size is 32MB.

编辑:根据http://www.cs.nyu.edu/exact/core/doc/*.txt,Window的最大堆栈大小为32MB。

#1


3  

Is static allocation obsolete?

静态分配是否已过时?

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

你没有进行静态分配 - 你正在进行自动分配,正如其他人所说的那样,你正在耗尽堆栈。

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

在C ++中基本上有三种为数据保留空间的常用方法:

  1. On the stack - these are called 'automatic variables', and they're what ordinary function-local variables are. Assuming your "int foo[][]" is local to main(), then this is what this is. Automatic data is limited by the available stack size, but it's very fast to allocate (basically zero time).

    在堆栈上 - 这些被称为“自动变量”,它们是普通的函数局部变量。假设你的“int foo [] []”是main()的本地,那么这就是这个。自动数据受可用堆栈大小的限制,但分配速度非常快(基本上为零时间)。

  2. Statically - these are either function-local or class variables which are proceeded by the word 'static', or they're variables defined outside functions or classes scope. Static data is reserved by the compiler There's no allocation time overhead, but the memory is reserved for the entire run-time of the application.

    静态 - 这些是函数本地或类变量,由“静态”一词继续,或者它们是在函数或类范围之外定义的变量。静态数据由编译器保留。没有分配时间开销,但内存是为应用程序的整个运行时保留的。

  3. On the heap - these are allocated with 'new' or 'malloc' or some mechanism which makes those calls internally. Allocation and release are slow compared with the first two, but you can have as much memory as the system will give you, and you can return it when you've finished with it.

    在堆上 - 这些分配有'new'或'malloc'或一些在内部进行调用的机制。与前两个相比,分配和释放速度很慢,但是你可以拥有系统给你的那么多内存,你可以在完成它后返回它。

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

这三个有微妙的变化 - 例如,alloca是1和3的混合,但这些是基础。

#2


1  

There might be a stack size setting you need to set that is defaulting to something small. It has been a long time since I needed to play with those settings.

可能存在需要设置的堆栈大小设置,该默认设置为默认值。我需要玩这些设置已经有很长一段时间了。

In the link options most likely

在链接选项中最有可能

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

我只有MSDEV 2005正在工作,但这里是关于堆栈链接器选项的说法:

The /STACK option sets the size of the stack in bytes. This option is only for use when building an .exe file.

/ STACK选项以字节为单位设置堆栈的大小。此选项仅在构建.exe文件时使用。

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

此选项指定虚拟内存中的总堆栈分配。默认堆栈大小为1 MB。链接器将指定值向上舍入为最接近的4个字节。

EDIT

Unless you are doing your own memory management I can't see why you would allocate this statically. But even in that case I would dynamically allocate memory up front...

除非您正在进行自己的内存管理,否则我无法理解为什么要静态分配它。但即使在那种情况下,我也会动态分配内存...

#3


0  

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. I think it's around 30MB in Windows, yes. What you have done here is, ironically, this very site's namesake. A Stack Overflow.

问题是方法中的非动态分配变量是在堆栈上分配的,并且最大堆栈大小比总可用内存小很多。我认为它在Windows中约为30MB,是的。具有讽刺意味的是,你在这里所做的就是这个网站的名字。堆栈溢出。

Edit: According to http://www.cs.nyu.edu/exact/core/doc/*.txt ,Window's maximum stack size is 32MB.

编辑:根据http://www.cs.nyu.edu/exact/core/doc/*.txt,Window的最大堆栈大小为32MB。