Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ pointer scope

Tags:

What happens when you have the following code:

void makeItHappen() {     char* text = "Hello, world"; } 

Does text go out of scope and get deleted automatically or does it stay in the memory?

And what about the following example:

class SomeClass {     public:       SomeClass();       ~SomeClass(); };  SomeClass::SomeClass() { } SomeClass::~SomeClass() {     std::cout << "Destroyed?" << std::endl; }  int main() {     SomeClass* someClass = new SomeClass();     return 0; } // What happend to someClass? 

Does the same thing occur here?

Thanks!

like image 730
Kevin Avatar asked Dec 31 '10 12:12

Kevin


People also ask

What is the scope of pointers in C?

A pointer is a variable that contains a memory location. Like all variables, it can be on the heap or the stack, depending on how it's declared. It's value -- the memory location -- can also exist on the heap or the stack. Typically, if you statically allocate something, it's on the stack.

What is scope pointer?

Scope Resolution Operator is used to access static or class members whereas this pointer is used to access object members when there is a local variable with same name.

Is pointer a variable scope?

Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time.

Do pointers have global scope?

A pointer variable can be in global or local scope and can also point to a variable that is in global, local, or no scope (as if it were coming off of the heap or addressing some DIO lines).


2 Answers

 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.

When the pointer goes out of scope, the memory pointer itself (4 bytes) is freed, and the string is still in the same place - constant memory.

For the latter:

SomeClass* someClass = new SomeClass(); 

Then someClass pointer will also be freed when it goes out of scope (since the pointer itself is on the stack too, just in the first example)... but not the object!

The keyword new basically means that you allocate some memory for the object on free store - and you're responsible for calling delete sometime in order to release that memory.

like image 51
Kos Avatar answered Oct 31 '22 05:10

Kos


Does text go out of scope

Yes! It is local to the function makeItHappen() and when the function returns it goes out of scope. However the pointed to string literal "Hello, world"; has static storage duration and is stored in read only section of the memory.

And what about the following example:

......
Does the same thing occur here?

Your second code sample leaks memory.

SomeClass* someClass = new SomeClass();

someClass is local to main() so when main returns it being an automatic variable gets destroyed. However the pointed to object remains in memory and there's no way to free it after the function returns. You need to explicitly write delete someClass to properly deallocate the memory.

like image 27
Prasoon Saurav Avatar answered Oct 31 '22 03:10

Prasoon Saurav