Can you free memory allocated by a std::string?

This is a discussion on Can you free memory allocated by a std::string? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by cpjust Elysia, I guess you haven't finished reading those books... Chapter 82. Use the accepted idioms to ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by cpjust View Post
    Elysia, I guess you haven't finished reading those books...
    Chapter 82. Use the accepted idioms to really shrink capacity and really erase elements
    It's called the "swap-trick".
    Nope, not yet. There's a lot to read and I have several books!
    Last edited by Elysia; 05-15-2008 at 11:57 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Oct 2002
    Posts
    72
    Quote Originally Posted by matsp View Post
    1MB isn't a whole lot in a modern machine - unless of course you have LOTS of your 1MB variables.

    My real question, however, is why you can't just destroy foo and use some other variable when you need a smaller one? That seems like the obvious solution - simpler, indeed, than messing about reserving less memory in foo.

    --
    Mats
    Well, I suppose that's the question. How do I destroy foo? It's a class member and that class needs to stick around & foo *may* need to be filled again with new data? Outside of making foo point to a dynamically allocated string (i.e. string* foo = new string) just so I can delete foo; is there some other way to destroy foo?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Refer to cpjust's example in post #13.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  2. Arrays and pointers to allocated memory
    By TriKri in forum C Programming
    Replies: 19
    Last Post: 07-23-2006, 10:52 AM
  3. Where are pointers allocated in memory?
    By sparks in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 12:52 PM
  4. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21