lacrosse1991
lacrosse1991

Reputation: 3162

Is there a way to automatically free all memory instead of doing a ton of free() functions?

I was just wondering if there was a way to free all memory at once in a program instead of doing a ton of free() functions, such as the following;

free(somevariable);

Its not really a problem, but just seems sort of excessive to do that for every variable if a program contains many of them, if there was sort of a catch-all to free all memory at the end, or a way to supply more than one variable at a time to free(), such as the following again, I would find that immensely helpful, thanks!

free(var1,var2,var3)

Upvotes: 0

Views: 800

Answers (5)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215547

You should stop and think about what you're asking for for a second. It doesn't make sense. If your only goal is to call "freeall" just before exiting, it's useless; terminating the program will already cause its entire memory space (including all allocations made by malloc) to cease to exist. If on the other hand you want to "freeall" at some other point before program termination, you've just violated all third-party code that might be linked into your program by deallocating its memory behind its back (how could it know your code is going to free its memory?)

With that said, there is something similar to what you want, which DOES work: talloc:

http://talloc.samba.org/talloc/doc/html/index.html

The idea of talloc is that you can make contexts of related memory allocations, and free them all together with a single call to talloc_free. It's not only very convenient, but also simplifies allocation/deallocation logic and should reduce the incidence of bugs due to incorrect deallocation (double free, leaks, etc.).

Upvotes: 1

Jay D
Jay D

Reputation: 3297

You need a smart pointer which takes care of memory management for your data structure.

Edit :- thanks to Oli , just realized its a pure C question. Smart pointers is a C++ construct.

In C you have to essentially have a free for every explicit allocation you do.

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263617

You need a call to free() for each call to malloc().

That doesn't necessarily mean that each malloc() call in your source code needs to have a corresponding free() call; it's the calls at run time that have to match, not the calls in your source code. But usually the source calls will match.

I should also note that free()ing all malloc()ed memory isn't absolutely required. When your program terminates, all allocated memory will be returned to the operating system. (The C standard doesn't guarantee this, but any OS you're likely to be using will do this.) But cleaning up after yourself is a good habit; for example, a program can become part of a larger program.

Upvotes: 4

Alexey Frunze
Alexey Frunze

Reputation: 62106

If your compiler supports __VA_ARGS__ (gcc does and the latest Visual C++ should), then you could do something like this:

#include <stdio.h>
#include <stdlib.h>

#define FREE(...) \
  { \
    void* pointers[] = { __VA_ARGS__ }; \
    size_t i; \
    for (i = 0; i < sizeof(pointers) / sizeof(pointers[0]); i++) \
      printf("freeing %p\n", pointers[i]); \
      free(pointers[i]); \
  }

int main(void)
{
  void *p1 = malloc(1), *p2 = malloc(2), *p3 = malloc(3);
  printf("p1=%p, p2=%p, p3=%p\n", p1, p2, p3);
  FREE(p1, p2, p3);
  return 0;
}

Output:

p1=005E17C0, p2=005E17E0, p3=005E17F0
freeing 005E17C0
freeing 005E17E0
freeing 005E17F0

Upvotes: 1

larsks
larsks

Reputation: 312530

Here:

#include <stdlib.h>
#include <stdarg.h>

void vfree(int count, ...) {
        va_list ap;
        int i;

        va_start(ap, count);

        for (i=0; i<count; i++) {
                void *ptr;
                ptr = va_arg(ap, void *);
                printf("arg %d = %p\n", i, ptr);
                free(ptr);
        }

        va_end(ap);
}

Call it like this:

vfree(3, ptr1, ptr2, ptr3);

That is, give it a count of pointers, and then the list of pointers, and it will call free on all of them.

NB: This is a terrible idea and a terrible implementation and you shouldn't use it.

Upvotes: 1

Related Questions