How can this work (array with dynamic size)

Hi all

this has been bothering me for a while. I sometimes see code like

void someFunc()
{
  int someArray[sizeY][sizeX];

  ...
  ...
}

where sizeX and sizeY are e.g. derived from user input or calculated on the fly.

I normally use fixed size arrays and I always thought that the compiler needed to know upfront how much space an array will occupy but that does not seem to be the case as it compiles. Can somebody please explain to me why this is valid and what actually happens under the hood?

In my view, this code can be seen as a dynamic memory allocation with variable size. And I consider it extremely dangerous if sizeX and sizeY result in a memory overflow.

If I use malloc(), I can at least test the result and act accordingly. With the above code that is not possible.

Sample code below (and yes, it shows ‘unexpected’ ( :wink: ) behaviour if when one enters e.g. “bb” in the serial monitor.

byte sizeX;
byte sizeY;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  if (Serial.available() >= 2)
  {
    sizeX = Serial.read() - '0';
    sizeY = Serial.read() - '0';

    someFunc();
  }
}

void someFunc()
{
  int someArray[sizeY][sizeX];

  char txBuffer[32];
  sprintf(txBuffer, "%p", someArray);
  Serial.println(txBuffer);


  for (byte y = 0; y < sizeY; y++)
  {
    for (byte x = 0; x < sizeX; x++)
    {
      someArray[y][x] = x * y;
    }
  }
}

Best I can tell, this kind of construction is called a "variable length array." It's apparently kosher in C99, but isn't part of standard C++. The GNU compliler, g++, supports variable length arrays as an extension.

Here's a description: http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

I wouldn't have expected this compile, and certainly wouldn't have expected it to work.

The size of the array is not being changed. A new array is being declared.

  int someArray[sizeY][sizeX];

Declares a new array the scope of which is the function in which it is declared. Once the function is exited the array no longer exists and its contents cannot be used.

I know that the array goes out of scope and hence a new array is created when the function is called again.

Point was that I always thought that array size had to be known at compile time. So what does the compiler create for this; probably time to dig through assembly again.

It also creates a serious memory issue, specially for newbies. As indicated, the use of malloc is a lot safer as one can check the return value.

Thanks for the replies.

I always thought that array size had to be known at compile time

Well, however it works, it seems OK as far as the compiler is concerned although the memory allocation could be "interesting"

probably time to dig through assembly again.

Good luck. Brew up plenty of coffee before you start.

You are allocating an array of arrays. For code that you understand, use malloc() to allocate the arrays. Use free() when you are done.

sterretje: Point was that I always thought that array size had to be known at compile time.

This has been allowed in C for non-global and non-static arrays since just about forever.