Is the above notation a shorthand for malloc?
No. When you declare an array as
T a[N];
what you get in memory looks something like this:
+---+
a: | | a[0]
+---+
| | a[1]
+---+
| | a[2]
+---+
...
There is no object a
apart from the array elements themselves. If declared locally to a function, like
void foo( void )
{
T a[N];
...
}
the memory for the array will (usually) be taken from the same store as any other local variable. That memory will also be released when the function exits.
When you allocate memory as
T *a = malloc( sizeof *a * N );
what you get in memory looks like this:
+---+ +---+
a: | | ------> | | a[0]
+---+ +---+
| | a[1]
+---+
| | a[2]
+---+
...
The pointer variable a
is allocated like any normal variable, and once the function exits it is destroyed, but the memory allocated for the array elements is not - that memory remains allocated until you explicitly call free
, so you either need to return that pointer to any calling function or free the memory before you exit the current function.