-
February 16th, 2009, 07:03 PM
#1
could not store class
Hi,
I have written a small class and tried to store in vector.
-------------------------------------------------------------------------
Code:
class subClas {
private:
Float_t fX;
Float_t fY;
Float_t fZ;evt->sub().size();
public:
subClas() { }
subClas(Float_t, Float_t, Float_t);
virtual ~subClas() {Clear(); }
....................................
..................................
};
class Clas{
private:
Float_t fEg;
vector<subClas*> sub;
public:
Clas() { }
virtual ~Clas() { Clear(); }
void Clear() { sub.clear(); }
void Init() { sub.clear(); }
void SetEg(Float_t eg) { fEg = eg; }
void Add(subClas* trk) { sub.push_back(trk); }
vector<subClas*> GetSub() const { return sub; }
Float_t GetEg() const { return fEg; }
};
and
Code:
subClas::subClas(Float_t rx, Float_t ry, Float_t rz)
{
fX = rx;
fY = ry;
fZ = rz;
}
I tried to store the object "Clas" as following:
Code:
vector<Clas*> array;
array.reserve(9999999);
Clas *cls;
subClas *cl1, *cl2;
for(int i=0; i<100; i++){
cls = new Clas();
cls->Init();
cls->SetEg(Eg);
cl1 = new subClas(x1, y1, v1); // x1, y1, z1, x2, y2, z2 are read from another data file
cl2 = new subClas(x2, y2, v2);
cls->AddTrack(cl1);
cls->AddTrack(cl2);
array.push_back(cls);
cls->Clear();
}
If I do this
Code:
Clas *evt = (Clas*)array.at(2);
cout << evt->GetEg() << endl; // it works
cout << evt->sub().size(); // it works
inside the above loop then it works. However, if I do this outside of the above loop then it prints evt->GetEg() fine but evt->sub().size() give zero.
I could not understand why? Your help is appreciated.
Thanks.
-
February 16th, 2009, 07:10 PM
#2
Re: could not store class
Originally Posted by manojg
Hi,
I have written a small class and tried to store in vector.
Please put together, in one module a small but complete program that demonstrates the error. This means no "..." in the post (as that could be anything that may be important to the program).
Programming is an exact science. This means we must know exactly what you have tried -- do not just describe what you've done, as your description does not translate to code.
Regards,
Paul McKenzie
-
February 16th, 2009, 07:19 PM
#3
Re: could not store class
I think that the question, the way it was asked, is incomprehensible. Please post a small compilable program that demonstrates the problem. I can't guess what you are doing wrong based on what you have posted.
I noticed that at the bottom of the for loop you are calling clear which will clear the vector inside the object that you just pushed into the array object. By the time the for loop exits all of the classes that you just pushed into the array will have empty vectors. You do realize that you are pushing pointers into the array right? You are not copying the objects into the array object.
The last snip of code you posted isn't even consistent with the rest of the program. The clas object doesn't have a sub() function. It only has a GetSub() function.
-
February 16th, 2009, 07:27 PM
#4
Re: could not store class
Do you also realize that you are reserving memory for 10 million pointers to cls objects? That is an awful lot of memory to reserve for this container. Since vectors must contain a block of contiguous memory, the program will need to find a very large chunk of heap in order to honor this request. Since pointers are probably 32 bit variables, that is 10 million times 4 bytes which equals 40 million bytes of memory that must be reserved and the entire block of heap memory must be contiguous.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|