D
dwightarmyofchampions
Hello. I have a couple questions about vectors of pointers. Here is my code:
#include <iostream>
#include <vector>
class Movie
{
public:
Movie(std::string title, int year);
~Movie();
private:
std::string title;
int year;
};
class Libraries
{
public:
Libraries(std::vector<Movie*> rhs_library);
~Libraries();
private:
std::vector<Movie*> video_library;
};
Movie::Movie(std::string rhs_title,
int rhs_year)
{
title = rhs_title;
year = rhs_year;
return;
}
Movie::~Movie()
{
return;
}
Libraries::Libraries(std::vector<Movie*> rhs)
{
this->video_library = rhs;
return;
}
Libraries::~Libraries()
{
return;
}
int main()
{
Libraries* MyLibrary = 0;
std::vector<Movie*> movies;
for (int i = 0; i < 10; ++i)
{
movies.push_back(new Movie("A Movie", 1979));
}
//vector of movies is built up, so add it to MyLibrary
MyLibrary = new Libraries(movies);
// ...
for (std::vector<Movie*>::iterator it = movies.begin();
it != movies.end();
it++)
{
delete *it;
}
delete MyLibrary;
std::cout << "END" << std::endl;
return 0;
}
So, movies is a vector containing 10 objects of type Movie*.
1. When we push_back a new Movie*, we are copying by value, so the new Movie parameter immediately goes out of scope. Is that a memory leak, and if sohow do I fix it?
2. To delete the ten elements in movies, do I leave the iterator for loop where it is or do I put it in the Libraries destructor, or do both?
3. Are movies and this->video_library pointing to the same memory? I guess that would be so, since that's what the line in the constructor is doing, pointing them both to the same address in memory. If so, how do I be sure that I'm deleting the elements being pointed to only once? If I change the Libraries destructor to the following:
Libraries::~Libraries()
{
for (std::vector<Movie*>::iterator it = this->video_library.begin();
it != this->video_library.end();
it++)
{
delete *it;
}
return;
}
…I get a segmentation fault. How do I fix this? Isn't there an if statement I can use or something?
#include <iostream>
#include <vector>
class Movie
{
public:
Movie(std::string title, int year);
~Movie();
private:
std::string title;
int year;
};
class Libraries
{
public:
Libraries(std::vector<Movie*> rhs_library);
~Libraries();
private:
std::vector<Movie*> video_library;
};
Movie::Movie(std::string rhs_title,
int rhs_year)
{
title = rhs_title;
year = rhs_year;
return;
}
Movie::~Movie()
{
return;
}
Libraries::Libraries(std::vector<Movie*> rhs)
{
this->video_library = rhs;
return;
}
Libraries::~Libraries()
{
return;
}
int main()
{
Libraries* MyLibrary = 0;
std::vector<Movie*> movies;
for (int i = 0; i < 10; ++i)
{
movies.push_back(new Movie("A Movie", 1979));
}
//vector of movies is built up, so add it to MyLibrary
MyLibrary = new Libraries(movies);
// ...
for (std::vector<Movie*>::iterator it = movies.begin();
it != movies.end();
it++)
{
delete *it;
}
delete MyLibrary;
std::cout << "END" << std::endl;
return 0;
}
So, movies is a vector containing 10 objects of type Movie*.
1. When we push_back a new Movie*, we are copying by value, so the new Movie parameter immediately goes out of scope. Is that a memory leak, and if sohow do I fix it?
2. To delete the ten elements in movies, do I leave the iterator for loop where it is or do I put it in the Libraries destructor, or do both?
3. Are movies and this->video_library pointing to the same memory? I guess that would be so, since that's what the line in the constructor is doing, pointing them both to the same address in memory. If so, how do I be sure that I'm deleting the elements being pointed to only once? If I change the Libraries destructor to the following:
Libraries::~Libraries()
{
for (std::vector<Movie*>::iterator it = this->video_library.begin();
it != this->video_library.end();
it++)
{
delete *it;
}
return;
}
…I get a segmentation fault. How do I fix this? Isn't there an if statement I can use or something?