I'm dealing with a map of vectors, and I'm making sure I clean up all the memory used. So, what exactly is the best way to handle this?

One person said to use clear() on each vector in the map and then use clear() on the map, but I don't think that deallocates the memory? Is that how I should handle this or is there another way?

Here's the part of my code I'm worried about.

main()
{   map <int, vector<string> > word;
    map <int, vector<string> >::iterator it;
    
    while (getline(cin, info) && info != "")
          { if ( isInVec(word[info.length()], info) == false) 
                 word[info.length()].push_back(info); }

    for ( it=word.begin() ; it != word.end(); it++ )
        { sort (word[(*it).first].begin(), word[(*it).first].end()); }
    echoMap (word);

    cout << endl;
    system("pause");
	return 0;
}

Recommended Answers

All 3 Replies

Answer is: Start again

You have

while (getline(cin, info) && info != "")
          { if ( isInVec(word[info.length()], info) == false) 
                 word[info.length()].push_back(info); }

Now word starts empty, so IMMEDIATELY, you access work[X]
were X is a large number. Then you push back to that place.
That is memory corruption.

info is a global string : That is horrible info!="" is better as !info.empty() I am not sure what you are tring to achieve but use .at instead of [] for your vector and it will throw exceptions when you make mistakes like above.

I'm dealing with a map of vectors, and I'm making sure I clean up all the memory used. So, what exactly is the best way to handle this?

As soon as the map goes out of scope, its destructor takes care of destructing all the vectors, whose destructors in turn destruct all the strings. So it all happens automagically (as long as you are not storing pointers to objects)

Answer is: Start again

You have

while (getline(cin, info) && info != "")
          { if ( isInVec(word[info.length()], info) == false) 
                 word[info.length()].push_back(info); }

Now word starts empty, so IMMEDIATELY, you access work[X]
were X is a large number. Then you push back to that place.
That is memory corruption.

info is a global string : That is horrible info!="" is better as !info.empty() I am not sure what you are tring to achieve but use .at instead of [] for your vector and it will throw exceptions when you make mistakes like above.

I apologize, info was not global, I just happened to delete it accidentally when I changing my code from one that reads from a file to one that reads from a keyboard. It should be declared within this function.

But, I don't see how that is memory corruption. All that line says is find the length of the word entered, and store it iat the end of the vector at that point. At least, that's how I understood it. And, when it runs through my function echoMap() which simply prints out what's in the map, it prints it out right. How would it print properly if I'm making an issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.