1

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.

Ausome
  • 203
  • 1
  • 6
  • In your program, why you are calling `return_name()` function recursively? This will never print anything and ultimately crash. Also, in `main()`, you need to put `;` after `char* name` statement. Please show the actual version of your program. – H.S. Nov 03 '17 at 02:23
  • @H.S. There is no real version of a program, I was simply reading about C storage classes and this came to my attention. – Ausome Nov 03 '17 at 02:45
  • @H.S. Also thank you for bringing the recursive call to my attention I actually didn't mean to put that in there it was left over from when I copied the printf statement – Ausome Nov 03 '17 at 03:21
  • 1
    [String literals: Where do they go?](https://stackoverflow.com/questions/2589949/string-literals-where-do-they-go) – Lundin Nov 03 '17 at 08:01

1 Answers1

3

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523