Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
What's the difference between a VLA and dynamic memory allocation via malloc?

I was curious with this:

What is the diference between:

const int MAX_BUF = 1000;
char* Buffer = malloc(MAX_BUF);

and:

char Buffer[MAX_BUF];
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

  • Case 1: In

     char Buffer[MAX_BUF];
    

    Buffer is an array of size MAX_BUF. The allocation technique is called VLA.

  • Case 2: In

    const int MAX_BUF = 1000;
    char* Buffer = malloc(MAX_BUF);
    

    Buffer is a pointer which is allocated a memory of size MAX_BUF which is 1000.

and, an array is not the same as a pointer, and C-FAQ has a Very Good collection detailing the reasons.

The major difference, in terms of usability and behaviour are:

  1. (1) is on stack, usually Note, while (2) is on heap, always.
  2. (1) has fixed size once allocated, (2) can be resized.
  3. (1) is allocated when the enclosing function is called and has the block scope OTOH, (2) is allocated memory dynamically, at runtime and the returned memory has a lifetime which extends from the allocation until the deallocation.
  4. (1) allocated memory need not be managed by programmer, while in (2) all malloc()d memory should be free()d. [Courtesy: Giorgi]

Note: Wiki

For example, the GNU C Compiler allocates memory for VLAs on the stack.

over 4 years ago · Santiago Trujillo Relatório

0

I will add a bit info in terms of memory management, in addition to what others said.

1) The main difference is here:

const int MAX_BUF = 1000;
char* Buffer = malloc(MAX_BUF);

You need to manage the allocated memory manually, e.g., free Buffer when you are done using it. Forgetting to free it (or freeing it twice) may lead to trouble.

2) With the second case:

char Buffer[MAX_BUF];

You don't need to free anything. It will get destroyed automatically. Hence you avoid the task of handling the memory - which is good. You should try to evaluate always which approach you need.

Some points.

  • Since second is allocated on stack, the first approach is taken also when large array needs to be created - since more memory is usually available on the heap.
  • Also if you create array using second approach for example in the method, the life time of the object will be that method - you will not be able to use that array outside that method. Whereas with dynamic allocation that is not the case.
over 4 years ago · Santiago Trujillo Relatório

0

char* Buffer = malloc(MAX_BUF);

creates a char pointer Buffer, dynamically allocates MAX_BUF bytes of memory via the malloc and makes Buffer point to the start of the allocated space. This memory is allocated on the heap.

char Buffer[MAX_BUF];

creates an array Buffer of size MAX_BUF which can hold a maximum of MAX_BUF characters. Note that you are creating a Variable Length Array (a feature introduced in C99) since MAX_BUF is a variable. This array may be created on the stack.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda