0

I have a simple problem but sometimes I'm a bit confused.

The first code:

Person *ptoPerson = new Person;
cout << ptoPerson->printMsg("Hi") << endl;
delete ptoPerson;

The second code:

Person p;
Person *ptoPerson = &p;
cout << ptoPerson->printMsg("Hi") << endl;
delete ptoPerson;

The problem occurs when deleting the pointer.
The first code works fine, and the pointer deletes, but the second code when implementing it a problem occurs at runtime.

Why the second code can't delete the pointer?
I think the pointer in the two cases is a pointer and can delete it, or am I wrong.

Lion King
  • 32,851
  • 25
  • 81
  • 143
  • 2
    Its the same code.. – tkausl Apr 25 '17 at 00:54
  • 3
    You can't delete something that wasn't allocated with `new`. – Barmar Apr 25 '17 at 00:54
  • If you use `&variable`, the variable goes away when you exit its scope, it can't be deleted with `delete`. – Barmar Apr 25 '17 at 00:55
  • @tkausl: Ok, why in the second code a problem occurs at runtime. – Lion King Apr 25 '17 at 00:56
  • 1
    You should familiarize yourself with the concept of [storage duration](http://en.cppreference.com/w/cpp/language/storage_duration). – Captain Obvlious Apr 25 '17 at 00:57
  • @Barmar: So, the pointer in the second code like a local variable and stored in the stack not the heap. – Lion King Apr 25 '17 at 01:01
  • Sorry everyone, I have updated the second code. – Lion King Apr 25 '17 at 01:04
  • 1
    Both pointers are local variables. The difference is what they point to. The first pointer points to an object that was dynamically allocated, the second points to an object with automatic storage. – Barmar Apr 25 '17 at 01:07
  • I don't want to be rude but someone who has 7K on stack asking such questions should be ashamed. Please learn basics about language before you start to using it. You can start from [here](http://en.cppreference.com/w/cpp/language/storage_duration) – Logman Apr 25 '17 at 01:12
  • @Logman: Only 78 score in C++, though. Yes, you are being rude. – Lightness Races in Orbit Apr 25 '17 at 01:13
  • @Logman Storage duration, particularly when it involves pointers and ownership can be unclear to developers coming from languages which completely manage the lifetime of objects. – Captain Obvlious Apr 25 '17 at 01:17
  • @CaptainObvlious Ok that true. But answering OP question do not resolve his problem with little knowledge about language he want's to learn/use. He probably do not bother to look for answer himself as it's common question about c++. He should read about tool that he wants to use, maybe do some tutorials. I can understand people that are new to this but he is not. I just want to point that to him that to learn new language he need to actually learn something not just ask random questions. – Logman Apr 25 '17 at 01:31
  • @Logman: I will read more, can you give me some articles about that subject? – Lion King Apr 25 '17 at 01:36
  • @LionKing: Your C++ book should explain it. – Lightness Races in Orbit Apr 25 '17 at 01:38
  • @BoundaryImposition: Unfortunately, I didn't see any explanation related to my question in my c++ book, and for that, I don't know the difference between the two cases. – Lion King Apr 25 '17 at 01:43
  • @LionKing unfortunately I learned c++ long time ago and now I can't recommended anything especially after c++11 but [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is a list of good books about c++. – Logman Apr 25 '17 at 02:02

4 Answers4

5

I think the pointer in the two cases is a pointer and can delete it, or am I wrong.

You are wrong.

You don't "delete a pointer"; you delete a thing that you created using new (by passing a pointer to that thing to the delete operator).

Here, you did not create anything using new, so there is nothing to delete.

Ideally your code would look like this:

Person p;
cout << p.printMsg("Hi") << endl;
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thank you, but I am still confused, do you mean this `Person *ptoPerson = &p;` equal to `Person p` stored in stack and it doesn't need to the delete? – Lion King Apr 25 '17 at 01:33
  • @LionKing: Yes. You can obtain a pointer to any object, but that changes nothing about the object. It doesn't magically make the object require manual deletion, or change how it was allocated, or move it around in memory. It's still got automatic storage duration ("in stack"), so it still doesn't need to be manually deleted. That you took a pointer to it is irrelevant! – Lightness Races in Orbit Apr 25 '17 at 01:36
  • @LionKing: Then feel free to ask follow up questions until you do understand. – Lightness Races in Orbit Apr 25 '17 at 09:04
1
Person *ptoPerson = new Person

This pointer is point to a heap memory area (new Person), which is allocated by programmer from heap. After the oject of ptoPerson point to becomes useless, you must delete it, otherwise it will cause memory leak.

Person p;
Person *ptoPerson = &p;

This pointer is point to a stack memory area, which is maintained by compiler. when the area is out of range of code, it will be automatically deleted by compiler. if you delete the memory area of stack manually, and the program will crashed, because it is an illegal operation for program.

More detail, please click to this blog C++ MEMORY ALLOCATION

licp
  • 56
  • 3
  • 1
    @LionKing: [And wrong](http://stackoverflow.com/a/10157210/560648), not to mention not actually explaining the core misconception of yours (which is that you think all pointers need to be "deleted"). Don't mistake brevity for usefulness. – Lightness Races in Orbit Apr 25 '17 at 09:02
  • 1
    I don't see how linking to a C#/.NET resource is useful on a C++ question. Most (if not all) of that page is wrong/misleading for C++. – Lightness Races in Orbit Apr 25 '17 at 09:03
  • @BoundaryImposition: The key of this problem is heap and stack, pointer points different memory area (stack or heap) have different result. and programmer can not delete the stack memory (the second pointer of this problem), which is maintained by compiler. – licp Apr 25 '17 at 09:37
  • @licp: _"The key of this problem is heap and stack"_ No, it isn't. Not only are you misunderstanding storage duration in C++ (instead focusing on archaic implementation-specific details; _"the stack and heap of memory have nothing to do with any programming language"_ exactly: nothing!), but you are not hitting the crux of the problem (as already explained). – Lightness Races in Orbit Apr 25 '17 at 09:45
  • @BoundaryImposition: Thanks for figuring out the problem of link. – licp Apr 25 '17 at 09:54
0

When a pointer is =to new that means that the pointer is allocating dynamic memory for its content, when a point is set to &variable that means that the pointer is pointing to that reference of that variable. Thus, you can delete dynamically allocated memory not variables.

Felipe Lopez
  • 396
  • 3
  • 11
0

This answer is explaining why in second example occurred error at runtime.

You're running into undefined behavior.

Community
  • 1
  • 1
Logman
  • 4,031
  • 1
  • 23
  • 35