Am I creating memory leaks?

Started by
8 comments, last by BcS 20 years, 6 months ago
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;
}
Advertisement
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]
You never used new or malloc anywhere in that bit of code.. so no.. no leaks

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)?
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
Value of good ideas: 10 cents per dozen.Implementation of the good ideas: Priceless.Machines, Anarchy and Destruction - A 3D action sim with a hint of strategy
Also, when you do the following:

string MyString = "One";
MyString = "Two";

the std::string class handles any cleanup the "One" string itself, so you don''t even need to think about it.

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
Value of good ideas: 10 cents per dozen.Implementation of the good ideas: Priceless.Machines, Anarchy and Destruction - A 3D action sim with a hint of strategy
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]
Value semantics in general make it hard to leak.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Thanks for your help, everyone!

This topic is closed to new replies.

Advertisement