Reputation: 33
I am a newbie at C++, and I am just starting to work with the container called classes.
I am working on a program where an object is defined, and is stored inside a vector.
The program is as follows, and then the description of the error I got.
This is "car.h"
#ifndef CAR_H_GUARD
#define CAR_H_GUARD
class car{
int v;//the velocity
int loc;//location
public:
//default constructor, copy constr., cp. assgnmt. optr., destructor
car();
car(const car& j);
car& operator=(const car& j);
~car();
void set_v(int); //sets velocity
void set_pos(int); //sets position
int get_v (){return((v));}//returns velocity
int get_pos(){return(loc);}//returns position
};
#endif
This is "car.cpp"
#include "car.h"
#include <iostream>
using namespace std;
//constructor
car::car() {
v=1;
loc=0;
}
//copy constructor
car::car(const car& j){
v=j.v;
loc=j.loc;
}
//copy assignment operator
car& car::operator=(const car& j){
v=j.v;
loc=j.loc;
return *this;
}
//destructor
car::~car () {
delete &v;
delete &loc;
}
//sets velocity
void car::set_v(int p){
v = p;
}
//sets position
void car::set_pos(int l){
loc = l;
}
I have tried to create the "move constructor", by having a copy assignment operator. This is for the following code involving a vector of my objects to work.
#include "main.h"
#include "car.h"
#include <iostream>
#include <vector>
using namespace std;
//update car position
void update (vector <car> &cr){
//changes state of cars, eg.
for (int c = 0; c<cr.size();c++){
cr[c].set_v(1);
cr[c].set_pos(100);
}
}
int main(){
//a vector of cars
vector<car> cat;
car j;
j.set_v(100);
j.set_pos(99);
cat.push_back(j);
//new cars enters the road
j.set_v(120);
j.set_pos(77);
cat.push_back(j);
j.set_v(80);
j.set_pos(55);
cat.push_back(j);
//updating cars
update(cat);
//output
for (int i = 0; i<cat.size(); i++){
cout << cat[i].get_v()<<" is the velocity of car "<<i<<".\n";
cout << cat[i].get_pos()<<" is the position of car "<<i<<".\n";
}
}
And I get the following error, which I looked up on the Internetz.
main(8852) malloc: * * * error for object 0x7fff5fbff630: pointer being freed was not allocated * * * set a breakpoint in malloc_error_break to debug Abort trap
Although it might be a compiler incompability (I wrote in Xcode... and uses g++, on mac 10.6.8 ...), the code however does give an output when there is only one car in the vector. When I tried to introduce the other cars, I receive only the malloc error. I was wondering whether I am missing something for the class definition, or that somehow I botched up the data-types, or pointer referencing. There is also an issue with the values outputted if I tried to non-trivially manipulate it with an update function.
I have even tried to pass instead a vector of pointers (to the class) to the update function; I still have trouble inserting new elements into the vector. This site was very helpful in helping me learn the class structure, and its pointers.
Thanks for reading this long problem.
Upvotes: 3
Views: 542
Reputation: 81684
One uses delete
only on things where one has used new
. You never delete
something that wasn't separately allocated by new
. These lines in your Car
destructor are... very destructive (pardon my pun):
delete &v;
delete &loc;
Delete these lines -- they're totally unnecessary and they're the source of your problem. Just about the only time you'd use delete
in a destructor is when you've used new
to allocate an object in your constructor -- or when a pointer-to-object is passed to your constructor with the understanding that your object is responsible for deleting the pointer when your object is destructed.
Upvotes: 2