Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a pointer is created in scope, what happens to the pointed to variable when the pointer goes out of scope?

The title says it all.

I found an old question that is essentially the same, but I needed a tiny bit further clarification.

In this question the accepted answer says:

char* text = "Hello, world"; 

Here an automatic variable (a pointer) is created on the stack and set to point to a value in constant memory, which means:

  • the string literal in "" exists through the whole program execution.
  • you are not responsible for "allocating" or "freeing" it
  • you may not change it. If you want to change it, then you have to allocate some "non-constant memory" and copy it there.

Is this saying that the pointer gets deleted, but not the data the pointer is pointing to? If I were to create 1,000,000 pointers to characters in a function, when they go out of scope would all of my memory be released? Or just the memory required to make the pointers, leaving the actual characters themselves behind to hog up all my memory?

like image 928
xcdemon05 Avatar asked Feb 13 '13 15:02

xcdemon05


People also ask

What happens to a variable when it goes out of scope?

Nothing physical happens. A typical implementation will allocate enough space in the program stack to store all variables at the deepest level of block nesting in the current function. This space is typically allocated in the stack in one shot at the function startup and released back at the function exit.

What happens when a variable of reference data type goes out of scope in C#?

These variables are referred to as local because they exist (and are visible) only during the lifetime of the method. They are said to have local scope. When the method ends, the variable goes out of scope and is destroyed. C# divides the world of types into value types and reference types.

What is the operation called that gets the value pointed at by a pointer?

Dereference operator (*) An interesting property of pointers is that they can be used to access the variable they point to directly. This is done by preceding the pointer name with the dereference operator (*). The operator itself can be read as "value pointed to by".

Do pointers get deleted out of scope?

The raw pointer is not deleted until all shared_ptr owners have gone out of scope or have otherwise given up ownership. The size is two pointers; one for the object and one for the shared control block that contains the reference count. Header file: <memory> .


1 Answers

The arrays of characters will hang around for the entire execution of your program because they have static storage duration. This doesn't mean you need to delete them - they're supposed to stay around for the entire duration of your program. In fact, calling delete on it will give you undefined behaviour. You can only delete something that was allocated with new.

The pointers themselves have automatic storage duration and are destroyed when they go out of scope. It's worth noting that the pointer has to be a const char* because the string literal gives you an array of const char. Consider:

void func()
{
  const char* str = "Hello";
}

The array of characters containing Hello\0 exists for the duration of your program. The pointer str only exists for the duration of that function. Nothing needs to be deleted here.

This makes a lot of sense if you think about it. All of these strings you write in your source code have to exist in your executable somewhere. The compiler usually writes these strings of characters into the data segment of your executable. When you run your program, the executable gets loaded into memory along with the data segment containing your strings.

If you have two string literals in your program that have the same or overlapping text, there's no reason the compiler can't optimize it into storing only one of them. Consider:

void func()
{
  const char* str1 = "Hello";
  const char* str2 = "Hello";
  const char* str3 = "lo";
}

The compiler only needs to write the characters Hello\0 into the executable once here. The first two pointers will just point to the H and the third will point to the second l. Your compiler can make optimizations like this. Of course, with this example, the compiler can make an even further optimization by just getting rid of the strings all together - they're not used in any way that contributes to the observable behaviour of the program.

So yes, if you have a million distinct string literals that in some way contribute to the observable behaviour of the program, of course they have to exist as part of your executable.

like image 186
Joseph Mansfield Avatar answered Sep 21 '22 02:09

Joseph Mansfield