什么是&QUOT所允许的最大大小; unsigned char型"数组在Visual C ++ 6.0? [英] What is the maximum allowed size of an "unsigned char" array in Visual C++ 6.0?

查看:708
本文介绍了什么是&QUOT所允许的最大大小; unsigned char型"数组在Visual C ++ 6.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于图形工作,我需要有unsigned char型的数组。它必须是3维的,与第一尺寸大小4,(第1字节蓝,第2个字节绿色,第3字节红,第四个字节未使用)。为640×480的图像一个简单的数组,然后像这样完成的:

For working with graphics, I need to have an array of unsigned char. It must be 3 dimensional, with the first dimension being size 4, (1st byte Blue, 2nd byte Green, 3rd byte Red, 4th byte Unused). A simple array for a 640x480 image is then done like this:

unsigned char Pixels[4][640][480]

但问题是,在运行时它总是立即崩溃的程序。它编译罚款。它连接的罚款。它没有错误或警告。但是,当它运行它立即崩溃。我有code许多其他的线条,但它这一次我发现,导致立即死机。它不象我没有足够的内存来保存这些数据。这只是数据量很小,刚好够一个640×480全彩色图像。但我只看到过这种立即崩溃,当一个程序试图读取或(用CopyMemory的API函数,其中源或目标部分或全部已定义的变量的存储空间之外为例)写入未分配的内存。但是,这是没有这样的存储器读或写操作。它是一个存储器分配的操作。这应该永远不会失败,除非没有足够的内存在计算机中。而我的电脑肯定有足够的RAM(没有现代计算机就没有了足够的RAM)。有人能告诉我为什么它被搞乱?这是一个众所周知的问题,VC ++ 6.0?

But the problem is, it always crashes the program immediately when it is run. It compiles fine. It links fine. It has no errors or warnings. But when it's run it immediately crashes. I had many other lines of code, but it is this one I found that causes the immediate crash. It's not like I don't have enough RAM to hold this data. It's only a tiny amount of data, just enough for a single 640x480 full color image. But I've only seen such immediate crashes before, when a program tries to read or write to unallocated memory (for example using CopyMemory API function, where the source or destination either partially or entirely outside the memory space of already defined variables). But this isn't such a memory reading or writing operation. It is a memory allocating operation. That should NEVER fail, unless there's not enough RAM in the PC. And my PC certainly has enough RAM (no modern computer would NOT have enough RAM for this). Can somebody tell me why it is messing up? Is this a well known problem with VC++ 6.0?

推荐答案

的Visual C ++默认的系统堆栈的程序1MB。您试图分配堆栈的大小数组是要破坏你的堆栈1200KB。您需要在堆上分配阵列。 的std ::矢量是这是你最好的选择。

Visual C++ by default gives programs 1MB of stack. The size the array you are trying to allocate on the stack is 1200KB which is going to bust your stack. You need to allocate your array on the heap. std::vector is your best bet for this.

using namespace std;
vector<vector<vector<unsigned char>>> A(4, vector<vector<unsigned char>>(640, vector<unsigned char>(480, 0)));

这看起来多一点混乱,但会做你初始化数组方面想要什么,意味着你不必担心内存泄漏。

This looks a bit more confusing but will do what you want in terms of initialising the array and means you don't have to worry about memory leaks.

另外,如果这不是一个选项,然后就可以通过传递 /堆栈增加堆栈大小:其次是字节连接器所需的堆栈大小。

Alternatively if this isn't an option then it is possible to increase the stack size by passing /STACK: followed by the desired stack size in bytes to the linker.

编辑:在速度上的利益,你可能希望使用一个单独的内存分配的块来代替:

in the interests of speed you may wish to use a single allocated block of memory instead:

std::unique_ptr<unsigned char [][640][480]> A(new unsigned char [4][640][480]);

这篇关于什么是&QUOT所允许的最大大小; unsigned char型&QUOT;数组在Visual C ++ 6.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆