Ski Mask
Ski Mask

Reputation: 353

How do I free up memory allocated implicitly in an array?

For dynamic memory allocation in C, you have deallocate (or free) the reserved memory before you can execute and compile your program. For example:

....
... = malloc(...)
...
free(...)
return 0;

But how does it work if I don't use dynamic memory allocation. For example, if I reserve 40000000 bytes of space using int array [10000000], how can I free up the memory later on in the program when I don't need it?

Upvotes: 0

Views: 156

Answers (3)

John Bode
John Bode

Reputation: 123488

For dynamic memory allocation in C, you have deallocate (or free) the reserved memory before you can execute and compile your program.

That's...confused. Dynamic memory only exists at runtime - it has nothing to do with compiling or executing the program. You free memory when you don't need it anymore in the running program.

As for things not allocated using *alloc...

If you define int array[10000000]; within the body of a function and without the static keyword, then array has automatic storage duration; the memory for it is reserved when you enter the function and released when you exit1, 2. If you define the array at file scope, outside the body of any function, or if you declare it with the static keyword, then array has static storage duration and memory will be reserved for it when the program starts and released when the program exits.


  1. Officially speaking, memory is only guaranteed to be reserved when you enter that variable's enclosing scope - that is, if you define the array in the body of a for loop, then the memory is only guaranteed to be available for the duration of that loop. However, as a practical matter, most compilers will allocate space for all locals at function entry and release it on function exit.
  2. Note that an array that large will probably cause a runtime error when you run the program - storage for local variables in an individual function is relatively limited.

Upvotes: 1

Roberto Caboni
Roberto Caboni

Reputation: 7490

There's a reason if it is called dynamic memory: it can be dynamically allocated/deallocated whenever you need it.

And there's a reason also if the opposite of dynamic memory is something... that's not dynamic: a static array like the one you mentioned cannot be deallocated.

What you need is the concept of lifetime of a variable:

  • If int array [10000000] is a global variable, it's lifetime is the whole life of the program
  • If int array [10000000] is within a block enclosed by curly braces { }, it is stored in the stack and it's life ends as soon as the execution exits the block
  • If within a block the variable is declared with static keyword (static int array [10000000];) its lifetime is the whole program life as well though its scope is limited to the block

Upvotes: 1

There are different ways to allocate memory in C.

  • Automatic storage duration (a.k.a. local variables) - allocated automatically when a block is entered (a function, if statement, loop, or whatever), and deallocated when the block is exited.
  • Static storage duration (a.k.a. global variables and static local variables) - allocated automatically at the beginning of the program, and deallocated automatically at the end.
  • Dynamic storage duration (a.k.a. the heap) - allocated with malloc and deallocated with free.

By the way, you don't have to deallocate everything before exiting the program. The operating system will deallocate all memory belonging to your program when it exits. (Otherwise you'd have to restart your computer a lot more often)

Upvotes: 1

Related Questions