?
Solved

Erasing a vector contents

Posted on 2003-03-30
4
Medium Priority
?
195 Views
Last Modified: 2010-04-01
Suppose I have a class with 2 vectors (defined in <vector>) as members of the class.
Throughout the methods of the class I insert elements (strings that are read from a file, not allocated with "new") into these vectors. Should I erase all the elements of these vectors in the destructor of the class? If so - why? aren't these string allocated on the stack?

Same question for a vector I declared in one of the class methods. Should I call erase on this vector at the end of the method?

Thank You.  
0
Comment
Question by:Makover
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 8233685
You don't need to do this because vector class destructor clears vector. Vector declared in class method may not be released as well.
But if you keep pointers as vector elements, you need to release all these pointers in destructor (or when the program exits the function).
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 8233984
Looks like you aren't really aware of what you are doing here. You most definately allocate new strings when inserting them into the std::vector. That's the reason why elements in a std::vector _need_ to have a copy constructor. The fact that you are reading strings from a file doesn't mean that you aren't creating new string objects either, unless you use some nifty memory-mapped file techniques.

A word of advice: When you are still new to a language, only use a subset that you are absolutely sure about how to use it. Always know what you are writing. With c++ this would most likely mean that you will be using it merely as a better c for a period of time.

.f
0
 
LVL 4

Accepted Solution

by:
Chase707 earned 140 total points
ID: 8234188
>>strings that are read from a file, not allocated with "new"

I think he means, that he is not using char*, and thus, not allocating explicitly using new, rather with std::string?

In either case, you are allocating memory, and the memory will be allocated on the heap, or on the 'free store', whatever you want to call it.

From stroustrup:

"an automatic String manages a sequence of characters on the free store and automatically frees that memory when it itself goes out of scope.  All of the standard containers can be conveniently implemented in this way."

Therefore, if you are using std::vector<std::string>, you are allocating on the heap, and you do not need to explicitly clear these vectors.

As AlexFM stated before, if you have a vector to pointers, std::vector<char *>, then you need to delete the memory explicitly, because the char* does not have a destructor that can free the memory.
0
 

Author Comment

by:Makover
ID: 8234201
Thanks AlexFM and fl0yd, All your answers were very helpful.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question