-3
char *str = "hi";

delete [] str;

I wonder why we can't free memory by delete operator? I'm getting an exception 'Runtime error', when doing this.

char *str = "hi"; automatically allocates memory and writes string in it.
So delete operator should work, or am I wrong with this assumption?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Higgs
  • 63
  • 2
  • 8
  • There is no (dynamic) memory allocation - str is just a pointer to a string literal. – Paul R May 10 '14 at 18:46
  • Your title suggests you want to ask about deleting memory allocated by the new operator. Your question doesn't involve the new operator. So what are you asking? – juanchopanza May 10 '14 at 18:47
  • @PaulR In the most general sense, there is (static and automatic allocation specifically), just not dynamic memory allocation. –  May 10 '14 at 18:47
  • @PaulR I have voted to reopen this question, because it's expressing a very commonly misconception how c++ works with such things. The answers may also be well suited to help others on research. Please provide appropriate duplicates, before closing this! – πάντα ῥεῖ May 10 '14 at 19:33
  • Many duplicates, e.g. http://stackoverflow.com/questions/15011015/c-strings-deletion-in-c – Paul R May 10 '14 at 19:43
  • @PaulR May be the question should have been to put on hold as a dupe in 1st place then. (to be honest: I tried to close vote as the 1st reaction as well, but found myself out of close votes for today. Thus I decided to write the answer and edit the OP) – πάντα ῥεῖ May 10 '14 at 20:17
  • 1
    Probably - finding duplicates can be hard work though, even when you know there are plenty. – Paul R May 10 '14 at 21:38

4 Answers4

4

I wonder why we can't free memory by delete operator?

You don't need to in this case!

char *str = "hi"; automatically allocates memory and writes string in it.

No it doesn't! This all happens at compile time (and should even leave you a warning).
You didn't allocate str, you assigned a pointer to a const char* literal (the compiler is allowed to place wherever the implementation wants to), thus you can't delete it.

Dynamic allocation as managed with new() and delete would mean something like

char* str = new char[3];
strncpy(str,"hi",3);

// ...
delete [] str;
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

The key thing to note from your question is

char *str = "hi"; automatically allocates memory and writes string in it

String literals are automatically allocated and automatically deallocated. You can't free the memory yourself.

Note that they will last for the lifetime of the program, not just the scope of the variable. See this SO answer.

Community
  • 1
  • 1
Daniel
  • 1,920
  • 4
  • 17
  • 35
1

delete is used to de-allocate objects originally allocated with new. delete[] is used to de-allocate arrays originally allocated with new[].

You didn't use new in your first line, so shouldn't use delete in the second.

Specifically,

char *str = "hi";

allocates memory for a pointer called str, which has automatic lifetime. Automatic here means the compiler takes care of it for you.

The pointer points to a string literal, "hi", which was statically allocated once, at compile time, and can never be changed. In fact, your pointer should be const.

Now, if we change your code to

std::string str = "hi";

a dynamic allocation does happen since std::string makes a copy of your string literal, but it also takes care of releasing that copy for you. It may well use new[] and delete[] internally.

Useless
  • 64,155
  • 6
  • 88
  • 132
0

That is a fair question. But there's more to the business of constant strings than meets the eye: the compiler and linker are free to maintain pools of read-only literals wherever they want.

In fact, if the str in your example is in automatic (function-local, stack) storage and you have a high-speed memory pool handler, you may be able to make a catastrophic mess by trying to free it and then reuse it. (Don't try this at home :-)

O. Jones
  • 103,626
  • 17
  • 118
  • 172