C ++, offset vectors, pointers and objects

I am working on a project as homework for my university system programming course. I am very confused about the issue of pointers, vectors, stacks and heaps.

Using C ++. I have to get a vector of objects that are courses, and those course objects have several different fields. I have done this:

vector<CoursesObject> coursevector; 

and then I created a class in the Courses class that contains the space left in the course and the name of the course fields. Now I want to add a new course:

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

Now I want to add it to the handler vector:

 coursevector.push_back(*theCourse); 

With all that 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 theCourse pointer to the handler vector, which points to the course object in the heap. Is this what I said correctly?

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 this is not a pointer. But haven't I added a POINTER to the course object in the cursor stream?

Please explain, I have to handle memory correctly, and it seems that I do not understand.

+4
source share
5 answers

You don’t need to use the new one 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. 
+4
source

it

 vector<CoursesObject> coursevector; 

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

 coursevector.push_back(*theCourse); 

you get a copy of CoursesObject pointed to by theCourse stored in the vector. You do not need to delete entries from the vector. Actually you cannot, because it does not contain pointers.

Your program will be much simpler if you simply avoid dynamic allocation:

 coursevector.push_back(CoursesObject(name, space)); 
+5
source

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

 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 

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

0
source

when you do this:

 coursevector.push_back(*theCourse); 

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

 vector<CoursesObject *> coursevector; 

Then you add the object:

 coursevector.push_back(theCourse); 

Now your code for deleting objects should work:

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

coursevector can only contain CoursesObject , not pointers to CoursesObject s, so you don't need to use the new operator (check @Mankarse's answer). But, if you still want to hold the pointers, change the cursor definition to

 vector<CoursesObject*> coursevector; 

and push_back pointer value as is:

 coursevector.push_back(theCourse); 
0
source

Source: https://habr.com/ru/post/1443833/


All Articles