여대엽학부생
여대엽학부생

Reputation: 39

Is it okay to free multiple dynamically allocated memories at once?

During my school assignments, I just got curious if it really frees as I expect.

#include "stdio.h"
#include "stdlib.h"

void main(){
    int* menu1 = (int*)malloc(sizeof(int));
    int* menu2 = (int*)malloc(sizeof(int));
    int* menu3 = (int*)malloc(sizeof(int));
    int* menu4 = (int*)malloc(sizeof(int));
    int* menu5 = (int*)malloc(sizeof(int));

    free(menu1,menu2,menu3,menu4,menu5);
    system("pause");
}

It still runs in VS2017, but I'm not sure that it's freed correctly.

Upvotes: 3

Views: 357

Answers (5)

user3344003
user3344003

Reputation: 21627

If this compiles you are using an olde compiler. What you are showing is a common error in C code.

What happens in this case the generated code looks something like this:

PUSH  menu5 ; which probably looks more like "menu5(SP)"
PUSH  menu4
PUSH  menu3
PUSH  menu2
PUSH  menu1
CALL _free

free () only expects one argument and it finds an argument: menu1. The extra arguments simply get ignored.

What would cause even bigger problems is to not pass enough arguments to a function. In that case the function being called would use random garbage on the stack as the argument. Something like

 fprintf (fp)

in rarely executed logging code could give you a crash.

Upvotes: 0

machine_1
machine_1

Reputation: 4454

Your code does not compile, because the function free() expects one argument, but you passed more than that; 5 to be exact.

You could make it compile by enclosing the arguments in parentheses:

free((menu1, menu2, menu3, menu4, menu5));

This allows all the arguments to be evaluated by using the comma operator, but only the last one will be used by the function free(). That is, only menu5 will be freed, and the rest won't.

You must call free() on each argument separately to release memory appropriately:

free(menu1);
free(menu2);
...
free(menu5);

Upvotes: 2

sg7
sg7

Reputation: 6298

The code as is, will not compile:

  free(menu1,menu2,menu3,menu4,menu5); /* error: too many arguments to function 'free' */

The function free is defined in header <stdlib.h> and takes only one parameter:

void free( void* ptr );

Parameters:

 `ptr` -    pointer to the memory to deallocate

Return value:

(none)

Function deallocates the space previously allocated by malloc(), calloc(), aligned_alloc, (since C11) or realloc(). If ptr is a null pointer, the function does nothing.

References:

C11 standard (ISO/IEC 9899:2011):
7.22.3.3 The free function (p: 348)

C99 standard (ISO/IEC 9899:1999):
7.20.3.2 The free function (p: 313)

C89/C90 standard (ISO/IEC 9899:1990):
4.10.3.2 The free function

Upvotes: 2

Omer Dagan
Omer Dagan

Reputation: 662

No.

https://linux.die.net/man/3/free

void free(void *ptr);

I code does not compile:

error: too many arguments to function ‘free’
  free(menu1,menu2,menu3,menu4,menu5);

Upvotes: 2

rhaport
rhaport

Reputation: 550

for each pointer you have, you allocated memory separately. By memory allocation, the system allocates a bit more that requested in order to safe some control information which should be used for different internal purposes and also later for deallocation.

Since each allocated memory knows only about itself and not about other allocated pieces of memory, you cannot free all together.

The function free expects only one argument (one pointer). Please see 7.22.3.3 in C standard.

GCC doesn't compile this code.

Upvotes: 2

Related Questions