简体   繁体   中英

Auto storage class duration in C

I'm trying to gain a deeper understanding of C and would like clarification of the auto storage class.

So an auto variable has a duration of automatic, so it stops existing at the end of the block it is called in.

This code:

int main(void){
    char* name;
    name=return_name();
    printf("Name is: %s\n Addr is: %p\n", name,name);
    return 1;  
}

 char* return_name(void){
    char* n="Waldo";
    printf("Name is: %s & Addr is: %p\n", n,n);
    return n;
}

Would output something like so:

Name is: Waldo & Addr is: 0xABCDEF
Name is: Waldo & Addr is: 0xABCDEF

So the variable n is forgotten, but the address is remembered through name . The data at the address is left untouched.

However, I read somewhere that after the function call ends since the variable duration was automatic; the memory address is released back to the program and it could potentially be overwritten by the program later? So if you want that address to remain reserved you would have to declare n with static.

I can't seem to find the source where I read this and I just want to be certain.

Thanks

EDIT: I didn't mean to recursively call return_name in return_name. I removed the line name=return_name in that function.

The variable n is forgotten, but the address is remembered through name .

You are definitely on to something very important here: indeed, variable n is out of scope, and the value it used to have is remembered only because you assigned the return value of n to name inside main .

The variable is a pointer, so there are two pieces of information in play here: the value of the string, and a variable that stores its address. The pointer gets destroyed, but the value remains in place. You can access the value as long as you have a pointer to it.

I read somewhere that after the function call ends since the variable duration was automatic; the memory address is released back to the program and it could potentially be overwritten by the program later?

That is definitely true about the variable n : the program can reuse its memory for other things that it needs. However, this is not true about the value to which n points: the value of the string literal "Waldo" is not in the automated storage, so it does not get released.

Your code is well-behaved, but you need to be very careful, because you can very easily make it undefined behavior by copying string literal into automatic memory:

char* return_name(void) {
    char n[] = "Waldo"; // <<== Changed
    name=return_name();
    printf("Name is: %s & Addr is: %p\n", n, (void*)&n[0]);
    // Dereferencing the return is undefined behavior
    return n;
}

One little change in the declaration of n leads to your program's behavior becoming undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM