H
hall
Hi.
I have some problems with a class i've written that allocates memory
dynamicaly. I want to put these objects into a std::vector, but it does
not work. My class looks (simplified) like this:
class Mem{
int *p, num;
public:
Mem(int a)(0),n(a){
cout<< "created "<<num<<endl;
}
~Mem(){
if (p) delete []p;
cout << "destroyed "<<num<<endl;
}
void alloc(int i){
if (!p) p=new int;
}
};
My first attempt of creating a vector<Mem> was according to:
using namespace std;
int main(int argc, char* argv[])
{
vector<Mem> mv;
for (int i=0; i<3; i++){
cout << i<<endl;
Mem tmp(i);
tmp.alloc(10);
cout << "pushed"<<endl;
mv.push_back(tmp);
};
return 0;
}
However, this will not work as I create a temporary object tmp and lets
it allocate memory, then makes a bitwise copy of it that is pushed into
the vector. After this, tmp goes out of scope and deletes the memory it
had allocating, leaving the object in vector pointing to nonexisting
memory. Not good. (also, (not surprisingly) the program crashes, not
good at all)
So, how should i do this? My only present idea is to do something like:
vector<*Mem> ptrV;
for (int i=0; i<3; i++){
Mem *p=0;
ptrV.push_back(p);
ptrV = new Mem(i);
ptrV.alloc(10);
}
(written on the fly, so it might contain some errors, just to show the
idea...)
But this leaves the problem of memoryleaks if im not careful to delete
all allocated memory manually before clearing or overwriting the vector.
I know there is a better way of doing this, but have no idea of what it is.
Also, i'd appreciate if someone could explain the output of the main()
function above. The output to console is:
0
created 0
pushed
destroyed 0
1
created 1
pushed
destroyed 0
destroyed 1
2
created 2
pushed
destroyed 0
destroyed 1
destroyed 2
<Then, the program crashes>
On e.g. iteration 2, why is object 0, 1, and 2 destroyed? An explanation
would hopefully help me understand this copy-creation a little better.
cheers
hall
I have some problems with a class i've written that allocates memory
dynamicaly. I want to put these objects into a std::vector, but it does
not work. My class looks (simplified) like this:
class Mem{
int *p, num;
public:
Mem(int a)(0),n(a){
cout<< "created "<<num<<endl;
}
~Mem(){
if (p) delete []p;
cout << "destroyed "<<num<<endl;
}
void alloc(int i){
if (!p) p=new int;
}
};
My first attempt of creating a vector<Mem> was according to:
using namespace std;
int main(int argc, char* argv[])
{
vector<Mem> mv;
for (int i=0; i<3; i++){
cout << i<<endl;
Mem tmp(i);
tmp.alloc(10);
cout << "pushed"<<endl;
mv.push_back(tmp);
};
return 0;
}
However, this will not work as I create a temporary object tmp and lets
it allocate memory, then makes a bitwise copy of it that is pushed into
the vector. After this, tmp goes out of scope and deletes the memory it
had allocating, leaving the object in vector pointing to nonexisting
memory. Not good. (also, (not surprisingly) the program crashes, not
good at all)
So, how should i do this? My only present idea is to do something like:
vector<*Mem> ptrV;
for (int i=0; i<3; i++){
Mem *p=0;
ptrV.push_back(p);
ptrV = new Mem(i);
ptrV.alloc(10);
}
(written on the fly, so it might contain some errors, just to show the
idea...)
But this leaves the problem of memoryleaks if im not careful to delete
all allocated memory manually before clearing or overwriting the vector.
I know there is a better way of doing this, but have no idea of what it is.
Also, i'd appreciate if someone could explain the output of the main()
function above. The output to console is:
0
created 0
pushed
destroyed 0
1
created 1
pushed
destroyed 0
destroyed 1
2
created 2
pushed
destroyed 0
destroyed 1
destroyed 2
<Then, the program crashes>
On e.g. iteration 2, why is object 0, 1, and 2 destroyed? An explanation
would hopefully help me understand this copy-creation a little better.
cheers
hall