What happens to memory space after the use of a local variable?

Asked

Viewed 87 times

4

At the end of the execution of a function its local variables are "destroyed", correct? The spaces in memory have any values, in C we can notice when ordering to print a variable that we did not initialize, will be shown a value that was not assigned by the programmer, because it was already allocated in that space.

If we declare a variable that happened to be assigned to it the space that belonged to the local variable "destroyed" and do not initialize it with any value, when we have printed this variable the value that will be shown is the value of the local variable that was "destroyed"?

When I talk about "destroy" the local variable I am referring to free the space in the memory where it resided and erase the content, or just free the space leaving the content to be superimposed by another who happens to use that space.

  • 3

    No "destruction" occurs, memory space simply becomes available for new allocations. If a new allocation occurs and no initial value is assigned, the existing memory garbage will be used.

  • The garbage is the content of the variable that occupied that place of memory?

  • What existed saved at the allocated memory position that may not match the allocation of a previously allocated variable in that region.

1 answer

8


Obviously it depends on the language and even the implementation of it if the specification of the language leave this book. In general local variables are allocated in the stack and at the end of the scope of its use (it may not be the function, it may be a smaller scope) the space becomes free for use by other variables, in general a new call to a function. There’s no erasure of the value there, as far as I know in any technology unless somehow an object is created thinking about it.

Some languages may use heap even for local variables, or even another mechanism. Usually in this case need to have an automatic release mechanism of the variable because the variable should not hold an object more than its lifespan. Re-release does not mean erasing the data.

Note that a variable is not usually destroyed because this is a concept of its code, variables do not exist at runtime, exist objects that are in memory positions that in your code you referenced through an identifier that we call variable.

In C the behavior is actually a declared and uninitialized variable taking the value that was there, not necessarily from a previous variable, simply the existing value will be used every time it references the variable without a new explicit assignment. Outside C++ virtually no other language acts like this, in general the specification determines that the even not explicitly initialized value has its value zeroed by the compiler. Or you can ban a compile code if there is no explicit initialization. C is considered Portable Assembly and does not have this type of concern.

You can see more about memory consumption of variables in How to see how much memory occupies such a variable in C++? And how to use the define?.

  • "In C the behavior is actually a declared and uninitialized variable taking the value that was there", It reminded me of a test in college, and it really happened, I thought it was bizarre at the time :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.