简体   繁体   中英

C++, vectors, pointers and objects confusion

I am working on a project as a homework for my university course of Systems Programming. I got really confused with the matter of Pointers, Vectors, stacks and heaps.

Using C++. I have to get a vector of objects which are courses, and those courses objects have several different fields. What I did was this:

vector<CoursesObject> coursevector;

and then I created my Courses Object class, which contains the space left in course and name of course fields. Now I want to add a new course, I do:

CoursesObject *theCourse = new CoursesObject(name, space);

now I want to add it to the handler vector:

coursevector.push_back(*theCourse);

With all I know, I created a vector of Courses objects on the stack, and made a new pointer to a new course that is on the heap, and added to the handler vector the pointer theCourse that points to the course object in the heap. Is what I said correct?

When I try to delete those course objects, I do:

for(int i=0; i<coursevector.size(); i++)
    delete coursevector.at(i);

which gives me an error that it is not a pointer. But haven't I added to the coursevector a POINTER to the course object?

Please somebody explain, I have to handle the memory correctly and it seems that I am not getting it right.

This

vector<CoursesObject> coursevector;

is a vector of CourseObjects , so it cannot hold CourseObject pointers. When you do this:

coursevector.push_back(*theCourse);

you get a copy of the CoursesObject pointed at by theCourse stored in the vector. You do not need to delete any entries from the vector. In fact, you can't, because it doesn't hold pointers.

You program would be much simpler if you just avoided the dynamic allocation:

coursevector.push_back(CoursesObject(name, space));

You do not need to use new at all.

//This vector stores CoursesObject objects,
//not pointers to CoursesObjects.
vector<CoursesObject> coursevector;
//Construct theCourse with automatic storage
//duration.
CoursesObject theCourse(name, space);
//Copy theCourse into coursevector
coursevector.push_back(theCourse);
//theCourse will be automatically destroyed.
//coursevector (and all the objects that it contains)
//will be automatically destroyed.

all of your objects are not dynamically allocated, so you cannot delete them at any time during the program. Remember that you can only delete object once they are dynamically allocated:

int Var1 = 0;  //cannot be deleted, scope and life will exist for the life of program
int * Var2 = new int; //CAN be deleted, dynamically allocated.

delete Var2; //free memory

You can however delete your last object, which is a pointer. I would grab the last element of the vector and call delete on it(which should be your pointer to the class).

when you do this:

coursevector.push_back(*theCourse);

actually you are dereferencing the pointer theCourse, so you are adding a copy of the object. You need to declare a vector of CourseObject pointers:

vector<CoursesObject *> coursevector;

Then you add an object:

coursevector.push_back(theCourse);

Now your code to delete the objects should work:

for(int i=0; i<coursevector.size(); i++)
    delete coursevector.at(i);

coursevector can hold only CoursesObject s and not pointers to CoursesObject s, so you needn't use the new operator (check @Mankarse's answer). But, if you still want to hold pointers, then change the definition of coursevector to

vector<CoursesObject*> coursevector;

and push_back the value of the pointer as it is:

coursevector.push_back(theCourse);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM