1

I'm talking about the lifetime of auto variables in C (storage classes).

If I have an auto variable, it depends on the parent block if it exists or not (once "control" goes out of the block, the variable gets destroyed in RAM).

When the parent block exits (the function or block that the variable is in), will the variable get destroyed in RAM (will it no longer occupy space in RAM)? Or is the variable present in RAM but we don't have access to it?

2
  • 3
    Technically variables are not "destroyed in RAM". The variable-name ceases to exists for the compiler, and its location in virtual memory (or RAM, depending on system) can be reused, but nothing is "destroyed". Commented Jan 5 at 8:51
  • What version of C? In the current version ISO C23, auto has a weird and dangerous meaning. Not to be confused with variables with automatic storage duration. Commented Jan 9 at 14:06

3 Answers 3

1

A common practice for compilers is to allocate memory for all auto variables of a function at the function’s entry point, simply by decrementing the stack pointer by the size of all the variables.

This is an easy way to handle all forms of block exits, especially in cases where "goto" is used.

So, in general, when control exits a block but stays within the function, the memory occupied by "auto" variables that have become inaccessible is not freed and retains the values of those variables.

Look at this example, which works with gcc and msvc. It is, we all agree, bad practice, and may not works with other compilers:

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


int main(int argc, char **argv) {
    int v1;
    int *p;
    v1 = 123;
    {
        int v2;
        p = &v2; // Keeps track of v2 memory
        v2 = 321;
        printf("v1=%d v2=%d\n", v1, v2);
    }

    // Access to v2 memory, which is out of scope,
    // still prints the good value: "*p=321
    printf("*p=%d\n", *p);

    return EXIT_SUCCESS;
}

Once again, this example is ONLY for knowledge: DO NOT APPLY THIS IN SCHOOL OR PROFESSIONNAL WORK.

Sign up to request clarification or add additional context in comments.

1 Comment

BIG Thanks for the time ❤️
1

If I have an auto variable, it depends on the parent block if it exists or not (once "control" goes out of the block, the variable gets destroyed in RAM).

C 2024 6.2.4 says the lifetime of an automatic object without variable length array type “extends from entry into the block with which it is associated until execution of that block ends in any way.”

Calling a subroutine only suspends execution; it does not end it. Execution ends when program controls flows out of the block, goto is executed, or a call is made to one of various routines that affect program execution such as exit or longjmp.

When the parent block exits (the function or block that the variable is in), will the variable get destroyed in RAM (will it no longer occupy space in RAM)? Or is the variable present in RAM but we don't have access to it?

C does not have concepts of destroying objects (except for some thread-related functions specified in C 2024 7.28, <threads.h>). An object is said to exist when storage is reserved for it. That does not need to be anything more than an accounting procedure; memory can be reserved for an object or released by adjusting the stack pointer, by updating the internal data structures malloc and free use to track memory, or by the compiler planning the use of memory.

So, most often, the memory of an object continues to be accessible, but it may be used for other purposes.

1 Comment

hey ! big thanks for the time ❤️
0

auto doesn't change lifetime rules. When a variable is declared in a scope, its lifetime ends when it goes out of scope. End of story. Whether or not the memory still physically exists in RAM is up to the implementation.

In any case, there is no need to use auto in C at all. See: Where is the C auto keyword used?

6 Comments

“When a variable is declared in a scope, it is destroyed when it goes out of scope” is incorrect. When one routine calls another, the calling routine’s identifiers are not in scope, but their objects continue to exist.
Yes, they are still in scope. en.cppreference.com/w/c/language/scope The scope doesn't end just because a routine is called. It ends when the block ends. The variables in the scope might not be visible inside of the called routine, but they are still in scope.
The page you link to does not say that an identifier declared in one function is in scope in another function that it calls. Scope is the region of source text in which an identifier is visible (can be used). If function A calls function B, function B cannot use the identifiers of function A. They are not in scope. Function B can use function A’s objects, if it knows their addresses, because the lifetimes of objects are portions of program execution during which the objects exist (have storage reserved for them)…
… The lifetimes of the objects persist even during execution of portions of the program corresponding to source code where the identifiers are not in scope.
@EricPostpischil "The scope of any identifier declared inside a compound statement, including function bodies, or in any expression, declaration, or statement appearing in if, switch, for, while, or do-while statement(since C99), or within the parameter list of a function definition begins at the point of declaration and ends at the end of the block or statement in which it was declared." A scope doesn't end when a function call is performed. Just because an identifier may not be visible does not mean it is not in scope. Scope and Visibility are separate things...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.