Как массивы переменной длины (VLA) занимают место в памяти?
Я заметил, что VLA не занимают непрерывное пространство памяти, может ли кто-нибудь подтвердить то же самое ??
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
моя программа работала только для нескольких начальных индексов VLA. Но после замены memcpy
на for
Loop моя программа работает нормально для всех индексов VLA.
memcpy(prevRowBuf,curRowBuf,width);
копируетwidth
байт, а неwidth
int
с. Вам нужноmemcpy(prevRowBuf,curRowBuf,width * sizeof curRowBuf[324]);
(324
, конечно, произвольно, вы также можете использовать0
, или-3
, илиINT_MAX
). - person Daniel Fischer   schedule 26.06.2013