-1
  • malloc is slow and allocates on the heap. Good for large objects or objects that need to get their lifetime extended beyond the function where they are created.
  • static allocation is on the stack. Allocation baked in by compiler so essentially free time-wise. Good for small objects. Risk of SO otherwise.

In a nutshell, I stand by these rules to decide how to allocate blocks of memory.

I work on embedded devices where time and space constraints are exacerbated, and I cringe whenever I see VLA as I don't know what the underlying implementation will do and whether it will make the right choice. So my rule of thumb is to stay away from them. Are there situations where it is preferable to use VLA rather than the classical malloc or static allocations, especially with respect to the embedded space?

Lolo
  • 3,935
  • 5
  • 40
  • 50
  • **Comments have been [moved to chat](https://chat.stackoverflow.com/rooms/253279/discussion-on-question-by-lolo-when-to-use-variable-length-array-vla); please do not continue the discussion here.** Before posting a comment below this one, please review the [purposes of comments](/help/privileges/comment). Comments that do not request clarification or suggest improvements usually belong as an [answer](/help/how-to-answer), on [meta], or in [chat]. Comments continuing discussion may be removed. – Samuel Liew Apr 22 '23 at 08:05

2 Answers2

2

I cannot think of a good use of VLAs in embedded space. I'm hard pressed to think of a good use in applications space.

I have used them in the past as working storage when tokenizing or manipulating strings:

void tokenize( const char *str )
{
  /**
   * Copy input string to temporary buffer and tokenize
   * that buffer, leaving the original string unmolested
   */
  char tmp[strlen( str )+1];
  strcpy( tmp, str );
  char *tok = strtok( tmp, " " );
  while ( tok )
  {
    // do something interesting with the token
    tok = strtok( NULL, " " );
  }
}

But it's not something I've done all that often.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

Automatic VLA are usually not very useful, but VM types have some uses:

  • Size check when passing array to function
#include <stdio.h>

void f(const size_t size, const int buf[static size]);

int main(void)
{
    int arr[50] = { 0 };
    f(10, arr);  // acceptable
    f(50, arr);  // correct
    f(100, arr); // *WARNING*
    return 0;
}
  • Allocation of multidimensional arrays with one malloc() call
// `n` and `m` are variables with dimensions
int (*arr)[n][m] = malloc(sizeof *arr);
if (arr) {
    // (*arr)[i][j] = ...;
    free(arr);
}
Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
  • IIRC, required support for VLAs in this context may arrive in the next C or is at least considered. Here, the VLA allocates no space, yet allows for size calculations and checking. – chux - Reinstate Monica Apr 21 '23 at 15:37
  • @chux-ReinstateMonica Indeed, C23 will requite support for VM types, while automatic VLA will remain optional – Jorengarenar Apr 21 '23 at 15:39