2

Can you release the memory of an array defined with static allocation?

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
Brandon Tiqui
  • 1,429
  • 3
  • 17
  • 35

5 Answers5

6

No, it is not possible to de-allocate statically allocated memory.

Depending on the language (for example C/C++, using pointers) you may be able to use the memory held by this array for other purposes, but doing so will only re-use the memory; memory won't be released per-se.

This said, this idea of reusing static memory for / with variables other than the variables originally defined there, is only suggested to help understand the nature of this type of allocation. In practical terms, and in particular as a novice, it makes absolutely no sense to have the need for such a feature:

  • either the variable is expected to have a lifetime as long as the program
    at which case it should be declared static
  • or the variable is not going to be needed at some time during program execution
    at which case it should be dynamically allocated (? shortly after/during program initialization) and released whenever appropriate.
mjv
  • 73,152
  • 14
  • 113
  • 156
3

No, static allocation means it's automatically allocated at the start of the program, and lives for the entire duration of the program, and then is automatically released at termination.

Alex Budovski
  • 17,947
  • 6
  • 53
  • 58
1

In short... no.

Think of this memory as being "application scoped" and thus allocated/deallocated following the life-cycle of the application.

jldupont
  • 93,734
  • 56
  • 203
  • 318
0

this is possible. static arrays are deallocated automatically before process finishes.

Vaka
  • 1
-1

no... its not possible but if you assign that array pointer to other pointer it will cause memory leak..

d3vdpro
  • 2,887
  • 4
  • 25
  • 29