c++ pointer scope

2020-02-21 10:52发布

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!

4条回答
SAY GOODBYE
2楼-- · 2020-02-21 11:19

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.

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-21 11:22
 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.

查看更多
孤傲高冷的网名
4楼-- · 2020-02-21 11:27

The variable text does go out of scope (however the string literal is not deleted).

For objects that you allocate with new (like your SomeClass), you need to explicitly delete them. If you want objects allocated like this to be automatically deleted, take a look at boost smart pointers (std::unique_ptr if your compiler is c++0x aware).

This will automatically delete the allocated object when the shared pointer goes out of scope.

Your code would then look like this:

int main(int argv, char **argv)
{
  boost::scoped_ptr<SomeClass> ptr(new SomeClass);
  // the object is automatically deleted
  return 0;
}

Note: In this particular example, you could also use std::auto_ptr (but this will be deprecated in c++0x).

Note 2: As was pointed out in the comments by Kos, it is in this case more appropriate to use boost::scoped_ptr or std::unique_ptr (c++0x). My answer first used boost::shared_ptr, which is more appropriate if you need to share ownership of a pointer between several classes for instance.

查看更多
地球回转人心会变
5楼-- · 2020-02-21 11:39

In the first example the string literal is stored in data segment of your executable.
In the second case you do not have to call delete (in your example program just terminates) since on program termination the heap is freed anyway for the process.
Note though that there are OS (as I have read) that you have to explicitly release heap even if the program terminates since it will not be cleaned up at termination for you.
Of course programmer is responsible for memory management in C++ and objects you create on heap should be deleteed once unneeded.

查看更多
登录 后发表回答