-6

I have tough problems while trying to de-allocate this pointer.

I'm having this error:

error for object 0x10007fd20: pointer being freed was not allocated

I have the following structure:

char * doSomething() //I cannot change this method.
{
     return "hello world";
};
int main ()
{
  char * var= doSomething();
  cout<< var<<endl;
  delete[] var;
};

Please consider that I cannot use string, because I'm not being provided with the doSomething method. As I'm using it a lot.. after a while the app collapses because of the RAM memory use.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DaWNFoRCe
  • 151
  • 2
  • 11
  • You didn't use `new[]` to allocate space for a string, and `"hello world"` is a static string. Why are you trying to `delete[]` something that you didn't have to allocate memory for? If the body of `doSomething` actually does something else, what does it do? – wkl Jun 16 '14 at 14:04
  • Don't shout. This **is** a duplicate. You should only ever `delete[]` something that was created using `new[]`. Read a C++ tutorial. – The Paramagnetic Croissant Jun 16 '14 at 14:05
  • If you have problems with `char*` and `delete`, you should immediately stop using it now. Use `std::string` instead of `char*`. Use `std::vector` instead of `T* = new T[n]` and `T` instead of `T* = new T`. You don't need (to see) pointers! – stefan Jun 16 '14 at 14:07
  • 1
    Regarding `char * doSomething() //I cannot change this method.`: You definitively have to. It's wrong. If someone gave it to you, you can blame him/her. – stefan Jun 16 '14 at 14:10
  • If the function only returns a pointer to a static string, then it has absolutely nothing to do with your memory problems. Your problem must be with something else. – molbdnilo Jun 16 '14 at 14:15
  • Reading between the lines, I would assume the doSomething() function as written in the example code is only a place holder. But to help you with what to do with the returned char pointer, we would need more info about the real function. If you are really supposed to delete the char pointer, there should be some notes about that in the documentation of doSomething(). One possible problem might be if doSomething() allocates the char* with malloc(). Should that be the case, you should use free() to release the memory. – Steffen Jun 16 '14 at 14:28

4 Answers4

4

You are trying to delete[] a static string. You only want to delete[] objects you have created with new[].

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
1

You can delete only what was allocated using the corresponding operator new. String literals have static storage duration. They are not dynamically allocated. According to the C++ Standard

8 Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

I see multiple potential issues:

  • you are using delete[] on a string literal, wrong, and that's why you are getting the error
  • you are converting a string literal to a char*, which is deprecated and insecure (since you can't modify a string literal)
  • you are returning a pointer to a local variable, in your situation this is allowed because you are working with string literals but in general

    int *function() { int x = 10; return &x; }

Should never be done, if you return a pointer to something in a function, be sure it is dynamically allocated or statically allocated but not allocated on stack.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • He's not returning a pointer to a local variable; a string literal is _not_ a variable. And while one often speaks of returning a pointer to a local variable, the actual problem is returning a pointer to an object which has auto lifetime. – James Kanze Jun 16 '14 at 14:30
  • @JamesKanze: I specified that in that case it works, since it's a literal but in general the assumption is wrong. I even specified that I'm not talking about local variables but about variables allocated on stack. The automatic storage class is the class for local variables. Which other automatic storage variables which are non local exist? – Jack Jun 16 '14 at 15:10
  • Local variables can be either automatic or static. And of course, temporaries aren't variables, but have an even shorter lifespan than automatic variables. – James Kanze Jun 16 '14 at 15:37
0

In my belief, string literals like "Hello world" is stored in the static section of your program instead of on the stack. That means, there is only one copy of "Hello world" in the memory, no matter how many times you call the function. And those calls should return just the same address. The RAM problem of your program is not caused by this issue.

Neo1989
  • 285
  • 3
  • 14