C ++: Tablica o zmiennej długości [duplikat]
To pytanie ma już odpowiedź tutaj:
Narzut na tablicę o zmiennej długości w C ++? 2 odpowiedziJak tablice o zmiennej długości (VLA) zajmują miejsce w pamięci?
Zauważyłem, że VLA nie zajmują ciągłego miejsca w pamięci, czy ktoś może potwierdzić to samo?
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];
}
}
}
With the twomemcpy
operacje, mój program działał tylko dla kilku początkowych indeksów VLA. Ale po wymianiememcpy
zfor
Loop mój program działa poprawnie dla wszystkich indeksów VLA.