Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

char* arr = “Nisse”; ?

Status
Not open for further replies.

d00ape

Programmer
Apr 2, 2003
171
SE
Exactly what happens if I write:

char* arr = “Nisse”;

Can I run:

delete arr;

Or

delete [] arr;

What is the best thing to do?
 
A1: No, you can't, because of arr pointer refers to a static storage of "Nisse" string literal (it may be write-protected storage or not, but it's not a dynamic storage from the heap)...

A2: See above...

A3: The best solution: Do nothing to delete string literal via its pointer!
 
delete arr;
Should follow an arr=new something. You dont do any new, so...

delete [] arr;
Should follow an arr = new something[...]. You dont do any new [...], so...

/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
If you like at the end of your program you can do:

arr = 0;

Thats a way of making sure the pointer is 'NULL'ed and put in a nice consistant state before exiting your program.

keyword 'delete' only needs to be used if you use the keyword 'new'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top