我需要创建一个大小为800x800的二维int数组。但是这样做会导致堆栈溢出(哈哈)。
我对C ++还不熟悉,所以我应该做一些像向量的东西吗?并且只需将2D数组封装到类中?
具体而言,这个数组是我图形程序中的zbuffer。我需要为屏幕上的每个像素存储一个z值(因此是800x800的大尺寸)。
谢谢!
你只需要大约2.5兆的空间,所以只使用堆内存就可以了。除非需要调整大小,否则不需要使用vector。请参考C++ FAQ Lite中有关使用“2D”堆数组的示例。
int *array = new int[800*800];
完成后不要忘记 delete[]
它。
到目前为止,所有的帖子都让程序员自己来管理内存。这是可以避免的,也应该避免。ReaperUnreal 的方法已经非常接近我会做的事情了,除了我会使用向量而不是数组,并且还会将维度模板参数化并更改访问函数 - 还有,只是我的个人意见,要将代码整理得更加清晰易懂:
template <class T, size_t W, size_t H>
class Array2D
{
public:
const int width = W;
const int height = H;
typedef typename T type;
Array2D()
: buffer(width*height)
{
}
inline type& at(unsigned int x, unsigned int y)
{
return buffer[y*width + x];
}
inline const type& at(unsigned int x, unsigned int y) const
{
return buffer[y*width + x];
}
private:
std::vector<T> buffer;
};
void foo()
{
Array2D<int, 800, 800> zbuffer;
// Do something with zbuffer...
}
Kevin的例子很好,但是:
std::vector<T> buffer[width * height];
应该是
std::vector<T> buffer;
如果需要扩展,当然可以添加运算符重载而不是使用at()函数:
const T &operator()(int x, int y) const
{
return buffer[y * width + x];
}
并且
T &operator()(int x, int y)
{
return buffer[y * width + x];
}
例子:
int main()
{
Array2D<int, 800, 800> a;
a(10, 10) = 50;
std::cout << "A(10, 10)=" << a(10, 10) << std::endl;
return 0;
}
你可以使用一个向量组成的向量,但这会增加一些开销。对于z缓冲区,更典型的方法是创建一个大小为800 * 800 = 640000的数组。
const int width = 800;
const int height = 800;
unsigned int* z_buffer = new unsigned int[width*height];
unsigned int z = z_buffer[y*width+x];
我可以创建一个800*800的一维数组。使用单个分配(如此)可能比分配800个不同的向量更为高效。
int *ary=new int[800*800];
class _2DArray
{
public:
int *operator[](const size_t &idx)
{
return &ary[idx*800];
}
const int *operator[](const size_t &idx) const
{
return &ary[idx*800];
}
};
int* operator[](size_t y) { return &ary[y*width]; }
然后你可以这样写:my2DArray[y][x]
虽然更好的做法是使用 operator():int& operator[](int x, int y) { return ary[y*width+x]; }
- Niall有一种类似C语言的做法:
const int xwidth = 800;
const int ywidth = 800;
int* array = (int*) new int[xwidth * ywidth];
// Check array is not NULL here and handle the allocation error if it is
// Then do stuff with the array, such as zero initialize it
for(int x = 0; x < xwidth; ++x)
{
for(int y = 0; y < ywidth; ++y)
{
array[y * xwidth + x] = 0;
}
}
// Just use array[y * xwidth + x] when you want to access your class.
// When you're done with it, free the memory you allocated with
delete[] array;
你可以将 y * xwidth + x
封装在一个类中,并使用简单的 get 和 set 方法(如果您想进一步学习 C++,可能还可以重载 []
运算符)。不过,如果你刚开始学习 C++,建议慢慢来,不要开始创建可重用的完全类模板,以便处理 n 维数组,这只会让你在刚开始时感到困惑。
一旦你开始进行图形工作,你可能会发现额外的类调用会减慢你的代码。然而,在应用程序速度不够快且你可以分析出时间损失的情况下,不要担心这个问题,而是让它更容易使用,避免可能不必要的复杂性。
我发现 C++ lite FAQ 对此类信息非常有用。特别是你的问题可以在其中找到答案:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.16
你可以做的一件事是改变堆栈大小(如果你真的想要将数组放在堆栈上),在VC中使用的标志是[/F](http://msdn.microsoft.com/en-us/library/tdkhxaks(VS.80).aspx))。
但是你可能想要的解决方案是将内存放在堆上而不是堆栈上,为此你应该使用一个vector
的vectors
。
下面这行代码声明了一个包含800个元素的vector
,每个元素都是一个包含800个int
的vector
,它可以帮助你避免手动管理内存。
std::vector<std::vector<int> > arr(800, std::vector<int>(800));
> >
),这是必需的,以便将其与右移运算符区分开来(在C++0x中将不再需要)。或者你可以尝试类似这样的东西:
boost::shared_array<int> zbuffer(new int[width*height]);
你应该也能做到这一点:
++zbuffer[0];
如果你只需要一个实例,你可以在静态存储(文件作用域或在函数作用域中添加 static
限定符)上分配数组。
int array[800][800];
void fn()
{
static int array[800][800];
}
这样就不会进入堆栈,也不必处理动态内存。
嗯,基于Niall Ryan的工作,如果性能是一个问题,你可以进一步优化这个数学问题并将其封装到一个类中。
所以我们将从一些数学开始。请注意,800可以写成2的幂次方:
800 = 512 + 256 + 32 = 2^5 + 2^8 + 2^9
因此,我们可以将我们的寻址函数写成:
int index = y << 9 + y << 8 + y << 5 + x;
。
class ZBuffer
{
public:
const int width = 800;
const int height = 800;
ZBuffer()
{
for(unsigned int i = 0, *pBuff = zbuff; i < width * height; i++, pBuff++)
*pBuff = 0;
}
inline unsigned int getZAt(unsigned int x, unsigned int y)
{
return *(zbuff + y << 9 + y << 8 + y << 5 + x);
}
inline unsigned int setZAt(unsigned int x, unsigned int y, unsigned int z)
{
*(zbuff + y << 9 + y << 8 + y << 5 + x) = z;
}
private:
unsigned int zbuff[width * height];
};