9

I have a string object in my C++ program declared as follows:

string str;

I have copied some data into it and done some operations. Now I want to delete the str object from the memory. I am not able to use delete operator since str is not a pointer. How do I delete that object from the memory to reclaim the memory allocated to it?

Thanks, Rakesh.

Rakesh K
  • 8,237
  • 18
  • 51
  • 64

4 Answers4

16

You don't have to. When the string goes out of scope, it's destructor will be called automatically and the memory will be freed.

If you want to clear the string right now (without waiting until it goes out of scope) just use str.clear().

Dean Harding
  • 71,468
  • 13
  • 145
  • 180
  • what if we can't have it go out of scope? we may still be able to control the amount of dynamic memory it allocates internally by changing its contents – newacct Jun 28 '10 at 06:42
  • 1
    This answer seems like the best one given the tone of the OPs post. I bet the OP is expecting C++ strings to work like Java or something like that, or has heard about how difficult memory/management is in C++ and is worried he's doing it wrong. – Omnifarious Jun 28 '10 at 06:59
  • Reading the SGI implementation, I don't think `clear` releases the memory... but other implementations might. – Stephen Jun 28 '10 at 07:08
  • 1
    Please have a look at sbi's answer. It states what is suspicious with this answer. – pmr Jun 28 '10 at 11:02
  • 1
    clear is not guaranteed to release memory and often doesn't - the capacity, in many implementations, remains the same. It just empties the string contents but the memory buffer stays the same size. – stinky472 Jun 28 '10 at 12:34
9
str.clear();

or

str = "";

However, as stated in the comments, this does not guarantee (and, in fact, is rather unlikely) to actually return heap memory. Also not guaranteed to work, but in practice working well enough is the swap trick:

std::string().swap(str);

Still, implementations employing the small string optimization will retain a few bytes of stack space (and str itself of course also lives on the stack).

In the end I agree with all comments saying that it is questionable why you want to do that. Ass soon as str goes out of scope, its data will be deleted automatically anyway:

{
  std::string str;

  // work with str

} // str and its data will be deleted automatically
sbi
  • 219,715
  • 46
  • 258
  • 445
  • Is string::clear actually required to set the capacity to 0? Otherwise it wouldn't have any effect on the amount of allocated memory and it seems to me that this is what op wants. – pmr Jun 28 '10 at 06:48
  • I think @pmr is right, `clear` is can be used to clear contents, but retain memory. Sometimes helpful to avoid reallocations within a loop. – Stephen Jun 28 '10 at 06:54
  • @pmr and @Stephen: Thanks for your comments. I tried to fix my answer. – sbi Jun 28 '10 at 07:12
  • 1
    You need to invert the swap trick -- `std::string().swap(str);` -- otherwise you're binding a temporary to a non-const reference. – avakar Jun 28 '10 at 08:19
  • @avakar: Dammit, I'd swear I had enough sleep last night! `` Thanks, I fixed it. – sbi Jun 28 '10 at 08:43
  • 1
    +1 for pointing out the problem with clear for this purpose and demonstrating the swap with temporary (aka clear-and-minimize) idiom. – stinky472 Jun 28 '10 at 12:33
  • As to 'why': if you were particularly obsessive you could clear out Valgrind's 'Still reachable' report for strings allocated in `main`. – EML May 23 '23 at 17:23
2

Add an Additional scope

{
  String str;
  ...
  ...
}

to ensure that str goes out of scope when you no longer need it. Remeber it could be tricky in terms of how other variables are also defined.

Progo
  • 3,452
  • 5
  • 27
  • 44
pv.
  • 484
  • 1
  • 4
  • 9
1

Now I want to delete the str object from the memory.

You can't. At least, you can't delete it completely. str is already allocated on stack (or in code segment if it is global variable), and it won't completely go away until you return from routine, leave the scope it has been created in (or until you exit the program - for global variable).

You can call .clear(), or assign empty string to it, but it doesn't guarantee that all memory will be freed. It guarantees that string length will be set to zero, but certain implementation may decide to keep part of originally allocated buffer (i.e. reserve buffer to speed up future operations, like += ).

Honestly, unless you have very small amount of available memory, I wouldn't bother. This is a very small object.

SigTerm
  • 26,089
  • 6
  • 66
  • 115