-3
string *str=new string;
delete str;

after delete object, can the occupied space for the str pointer self exist?

jiafu
  • 6,338
  • 12
  • 49
  • 73
  • Generally speaking, you should not use "new" with `std::string` (or other sting classes). That contradicts the purpose they were invented for. – firegurafiku Mar 15 '15 at 16:54

4 Answers4

3

Yes, of course. The variable str, of type string *, is still in scope. It will cease to exist as soon as its current scope is exited. For example at the end of the function or { }-delimited block which contains these instructions.

This is why it is good practice to stop pointing to the adress where new string was once allocated, and which now it is no longer valid. It's always good for an existing to point to either valid memory or NULL.

string *str = new string;
delete str;
str = NULL;

Note that after the third line you can now ask "is str pointing to a valid memory location?", i.e. "can I dereference it safely?".

   if (str != NULL) ...

Without setting the pointer to NULL, on the other hand, you couldn't possibly do this kind of check. You should just remember that after the delete instruction it is now invalid to dereference str, and hence not do it. Clearly this is calling for trouble.

Although correct, this kind of code (a new locally matched with a delete, and any living pointer set to NULL after deleting its target memory location) is quite vulnerable and error prone in case of future changes. For example, what if the function isn't so simple, and in the path between new and delete you have conditionals and loops? What if you passed str to another function as a parameter? What did this function do with the pointer: copy it, clone the object it points to...?

A technique called RAII - Resource Allocation Is Initialization (see this question) helps to create designs that prevent such kinds of errors as memory leaks and memory access violations.

Note: Slightly more intuitive, although less orthodox names for RAII have been suggested on this site: UDSTMR - Using Destructor Semantics To Manage Resources, or UTSTTC - Using The Stack To Trigger Cleanup. See this answer.

Community
  • 1
  • 1
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
2

Delete operator of C++ does two things:

  • Calls object's destructor
  • Marks the part of memory occupied by the object as "free", such that it can be reused later for another object.

Usually, the data kept by class in memory remains for a while, but it's almost certain, that at some moment someone will overwrite them, so one should not rely on their existence and cohesion.

The pointer stored in str variable is kept and still points to that part of memory. You can reuse this pointer and allocate another string by calling new operator again.


As a curiosity, the old DOS game SimCity had a bug, which caused deallocated memory to be used after freeing. Because the game was so popular, Windows designers prepared a specific rule for this game, which allowed it safely to use deallocated memory, such that it could have been run from within Windows.

Spook
  • 25,318
  • 18
  • 90
  • 167
1

After

delete str

str still exists. This line of code releases the memory which str points to, so you can new it again.

BenC
  • 8,729
  • 3
  • 49
  • 68
Aryan
  • 2,675
  • 5
  • 24
  • 33
0
int main()
{

string *str=new string;
delete str;

}

What this does is,

create a container named str which is capable of pointing to a string. Now, when you do delete str the actual string gets deleted. But the pointer can still point to that memory location. (You might not be allowed to access it though). This pointer's scope is till the } of main and will be destroyed(memory allocated released on stack) when you are done with main

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59