Archived

This topic is now archived and is closed to further replies.

BcS

Am I creating memory leaks?

Recommended Posts

I''m trying to understand exactly what happens when I replace or erase objects from sequences. I replace an object in an array. I replace an object in a vector. And I erase objects from a vector. Am I creating memory leaks by using objects in this way? Thanks. Here''s the code...
#include <string>
#include <vector>

int main()
{
    string myArray[] = {"zero", "one", "two"};
    myArray[0] = "first"; // memory leak?  what happens to the string "zero"?


    vector<string> myVector;
    myVector.push_back("zero");
    myVector.push_back("one");
    myVector.push_back("two");
    myVector[0] = "first";  // memory leak?  what happens to the string "zero"?

    myVector.clear(); // what happens to all of the string objects?


    return 0;
}

Share this post


Link to post
Share on other sites
quote:
Original post by dmikesell
No, you''re not creating leaks. std::string handles the re-assigning of values with operator= and cleans up its memory with clear().

--
Dave Mikesell Software & Consulting

[edited by - dmikesell on October 16, 2003 6:14:23 PM]



Okay, what if I use objects of a class I created instead of strings? Would I need to write some member function to handle the re-assigning of values with the = operator to clean up memeory (which would the object''s destructor)?

Share this post


Link to post
Share on other sites
quote:
Original post by Maega
You never used new or malloc anywhere in that bit of code.. so no.. no leaks


That about sums it up. If you create objects of your own class, each call to push_back simply copies the instance into the vector structure, which handles and cleans up itself just fine. The objects of your own class are cleaned up when the function in which they are instantiated goes out of scope, unless you dynamically allocated them.

Value of good ideas: 10 cents per dozen.
Implementation of the good ideas: Priceless.

Proxima Rebellion - A 3D action sim with a hint of strategy

Share this post


Link to post
Share on other sites
No, no leaks there.

myVector[0] = "first"; // memory leak? what happens to the string "zero"?

The original char string "zero" is static data compiled into your executable, so it is loaded into memory with it, and freed when your program ends. When you initialize the vector element, a memory buffer is allocated by std::string's constructor and the string is copied in it. When you reassign it, std::string's operator will either copy it into the old buffer, or free it and allocate a new.

myVector.clear(); // what happens to all of the string objects?

Each is destroyed , causing std::string's destructor to be invoked on each of the objects in the vector. The destructor will free any buffer associated with the object.

[edited by - CWizard on October 16, 2003 7:15:48 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by BcS
Okay, what if I use objects of a class I created instead of strings? Would I need to write some member function to handle the re-assigning of values with the = operator to clean up memeory (which would the object''s destructor)?



Yes, you need to make sure your object has a good copy ctor and a good assignment operator. If you do not use pointers in your object, the default copy ctor and assignment operator should be fine. If you do have any pointers in your class, you need to write a copy ctor and assignment operator.

Share this post


Link to post
Share on other sites