?
Solved

Erasing a vector contents

Posted on 2003-03-30
4
Medium Priority
?
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

762 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