C ++:变长数组 [英] C++ : Variable Length Array
问题描述
如何变长数组(VLA)取内存空间?
How does Variable Length arrays (VLA) take space in memory?
我观察到,沃拉斯不采取连续的内存空间,任何人都可以请确认相同??
I have observed that VLAs do not take continuous memory space, can anyone please confirm the same??
void func(const IplImage *imgSrc, IplImage *imgDest)
{
uchar *data = (uchar *)imgSrc->imageData;
// get the image data
int height = imgSrc->height;
int width = imgSrc->width;
int step = imgSrc->widthStep;
int stepSobel = imgDest->widthStep;
//Calculate Sobel of Image
uchar *dataSobel = (sobelStart + stepSobel);
//**Declaration of VLAs**
int prevRowBuf[width],curRowBuf[width],nextRowBuf[width];
for(int j=1;j<(width-1);j++)
{
prevRowBuf[j] = data[j+1]-data[j-1];
curRowBuf[j] = data[step+j+1]-data[step+j-1];
}
// Some opencv processing
for() // Some Conditions
{
//memcpy(prevRowBuf,curRowBuf,width);
//memcpy(curRowBuf,nextRowBuf,width);
//Replaced with
for(int i=0 ; i< width; i++)
{
prevRowBuf[i]=curRowBuf[i];
curRowBuf[i]=nextRowBuf[i];
}
}
}
随着两个的memcpy
的操作,我的计划是只为沃拉斯的几个指标开始工作。
但随着替换
循环我的程序后,于沃拉斯的各项指标正常工作。 的memcpy
为
With the two memcpy
operations, my program was working only for few starting indexes of VLAs.
But after replacing the memcpy
with for
Loop my program is working fine for all the indexes of VLAs.
推荐答案
首先,C ++的现行标准没有沃拉斯。然而,C ++ 5月17日。
First off, the current standard of C++ has no VLAs. However, C++17 may.
我观察到,沃拉斯不采取连续的内存空间,任何人都可以请确认是否相同?
I have observed that VLAs do not take continuous memory space, can anyone please confirm the same?
没有,那是错的。沃拉斯会占用连续空间。这空间通常(总是?)来自于栈,而不是堆内存,就像大小固定的C数组。
No, that is wrong. VLAs will take up continuous space. That space usually (always?) comes from the stack rather than the heap memory, just like statically sized C arrays.
同样对于 GCC扩展VLAS 的真
这篇关于C ++:变长数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!