0

可変長配列 (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];
        }
    } 
 }

2 つのmemcpy操作では、私のプログラムは VLA のいくつかの開始インデックスに対してのみ機能していました。memcpyしかし、をループに置き換えた後、for私のプログラムは VLA のすべてのインデックスに対して正常に動作します。

4

1 に答える 1

4

まず、C++ には VLA がありません。GCC はそれらを非標準の拡張機能として実装しています。

さて、上記のGCC拡張機能のコンテキストであなたの質問に答えるには:

私は、VLA が継続的なメモリ空間を必要としないことを観察しました。誰かが同じことを確認できますか?

いいえ、それは間違っています。VLA は継続的なスペースを占有します。そのスペースは通常 (常に?) 静的サイズの C 配列と同様に、ヒープ メモリではなくスタックから取得されます。

于 2013-06-26T10:49:26.850 に答える