-4

I have tried to find an answer but couldn't see anything straight forward.

How do I free the allocated memory in the next snippet code:

const char* attStr = strdup(OtherCharStr); string str(attStr, strlen(attStr)); delete str; //???

AstroCB
  • 11,800
  • 20
  • 54
  • 68
eladyanai
  • 868
  • 1
  • 11
  • 30
  • 3
    Use `free(attStr);` in this case. – πάντα ῥεῖ Aug 31 '14 at 18:13
  • 6
    Don't use `strdup`. Just do `std::string str(OtherCharStr);`. `std::string` already copies it. – Captain Obvlious Aug 31 '14 at 18:26
  • @CaptainObvlious, but for some reason when i print the output i get garbage, i though that it is because the allocated memory by `str::string str(OtherCharStr)` is freed once i leave the function. – eladyanai Sep 01 '14 at 08:32
  • _"freed once i leave the function"_ - Your question doesn't include any information about it's use or an example of it returning the value from a function. Overall a **very** poor question. Return the `std::string` by value and let the compiler handle the optimization (RVO/NRVO). – Captain Obvlious Sep 01 '14 at 15:28
  • @CaptainObvlious, my question is good, your answers are garbage! "Don't use strdup. Just do std::string str(OtherCharStr);." i didn't ask you for alternative way to copy the string, I specifically asked how to free the allocated memory! If you don't have good or relevant answers then just go away. – eladyanai Sep 08 '14 at 13:24
  • Your comments make me sad. Good luck in achieving your future failures though. – Captain Obvlious Sep 08 '14 at 19:34

2 Answers2

3

C++ uses idiom called RIAA - Resource Acquisition Is Initialization. It means that object lifetime is driven by variable scope.

{
    std::string s("foo"); // variable s declaration and string initialization
    do_some_stuff(s);
    // end of scope of variable s - it is destroyed here
    // no need to free(s) or whatever
}
// variable s and the string doesn't exist here, no memory for it is allocated

This applies only for C++ objects that maintain its resources properly (are freeing them in the destructor). Simple pointers doesn't do it - you have to free them yourself:

const char *attStr = strdup(...);
// do something with attStr
free(attStr); // because strdup() documentation says you should free it with free()

Also notice that C++ uses new and delete rather than malloc() and free():

std::string *strPointer = new std::string(...);
// RIAA doesn't work here, because strPointer is just plain pointer,
// so this is the case when you need to use free() or delete
delete strPointer;

I recommend to read something about smart pointers which are deleting the object they point to automatically. I'm getting pretty far from the original question but this topic is important to understand how C++ works.

Community
  • 1
  • 1
Messa
  • 21,202
  • 4
  • 52
  • 73
1

You need to release attStr not the c++ string that will release its resources alone.

void func()
{
    const char* attStr = strdup(OtherCharStr);
    string str(attStr, strlen(attStr));
    free(attStr);
}//here str will release its own resources

Also you can do string str = OtherCharStr; an that's it. Only check what happens with OtherCharStr

dariogriffo
  • 3,751
  • 1
  • 15
  • 31