This is totally strange. I have code in which I read some parameters from a file, and I store them in two stl vectors. I have atoms and residues, and each atom holds a pointer to its remainder. After reading, after declaring a variable, it looks like the values ββin memory have changed:
atoms [0] .resid: 0x96fc250
& (atoms [0] .resid-> ID): 0x96fc25c
** (atoms [0] .resid-> ID): 1 **
atoms [1] .resid: 0x96fc250
& (atoms [1] .resid-> ID): 0x96fc25c
** (atoms [1] .resid-> ID): 1 **
atoms [2] .resid: 0x96fc3ec
& (atoms [2] .resid-> ID): 0x96fc3f8
(atoms [2] .resid-> ID): 2
atoms [3] .resid: 0x96fc3ec
& (atoms [3] .resid-> ID): 0x96fc3f8
(atoms [3] .resid-> ID): 2
---------------------------------------
atoms [0] .resid: 0x96fc250
& (atoms [0] .resid-> ID): 0x96fc25c
** (atoms [0] .resid-> ID): 891301941 **
atoms [1] .resid: 0x96fc250
& (atoms [1] .resid-> ID): 0x96fc25c
** (atoms [1] .resid-> ID): 891301941 **
atoms [2] .resid: 0x96fc3ec
& (atoms [2] .resid-> ID): 0x96fc3f8
(atoms [2] .resid-> ID): 2
atoms [3] .resid: 0x96fc3ec
& (atoms [3] .resid-> ID): 0x96fc3f8
(atoms [3] .resid-> ID): 2
Here is the code. I really don't know what I did wrong.
#define FT_GRO 1 using namespace std; class residue{ public: residue(){} residue(const residue& r){atoms=r.atoms; ID=r.ID; name= r.name;} residue(int n, string s) {name=s;ID=n;} public: vector<class atom*> atoms; int ID; string name; atom& addAtom(atom& a) { atoms.push_back(&a); return a;} }; class atom{ public: atom(){} atom(const atom& a){ID=a.ID,name=a.name,coord=a.coord,resid=a.resid ;} atom(const int anum, const string aname, const point3D& p, residue& res){coord=p; name=aname; resid=&res; ID=anum;} ~atom(){} public: point3D coord; int ID; string name; double distance(point3D& p) {return coord.distance(p);} double distance(atom& p) {return coord.distance(p.coord);} class residue* resid; }; int main(){ vector<atom> atoms; vector<residue> residues; double box1,box2,box3,x,y,z; char l[256]; int nr,na; string sr,sa; int lastResNum = -1; string lastResName(""); int nAtomsIn=4; for(int i =0; i<nAtomsIn;i++){ cin.getline(l,255); istringstream ssatm(l,ios::in); ssatm >> setw(5) >> nr >> setw(5) >> sr >> setw(5) >> sa >> setw(5) >>na >> setw(8) >> x >>setw(8) >> y >>setw(8) >> z; if (lastResNum!=nr || sr!=lastResName){ residues.push_back(residue(nr,sr)); } point3D p(x,y,z); atoms.push_back( atom(na,sa,p,residues.back()) ); residues.back().addAtom(atoms.back()); cout << "atoms["<<i<<"].resid :" << atoms[i].resid << endl; cout << "&(atoms["<<i<<"].resid->ID) :" << &(atoms[i].resid->ID) << endl; cout << "&(atoms["<<i<<"].resid->ID) :" << (atoms[i].resid->ID) << endl; lastResNum=nr; lastResName=sr; } cout << "---------------------------------------"<<endl; cin.getline(l,255); istringstream ssbox(l); ssbox >> setw(10) >> box1>>setw(10) >> box2>>setw(10) >> box3; for(int i =0; i<atoms.size();i++){ cout << "atoms["<<i<<"].resid :" << atoms[i].resid << endl; cout << "&(atoms["<<i<<"].resid->ID) :" << &(atoms[i].resid->ID) << endl; cout << "&(atoms["<<i<<"].resid->ID) :" << (atoms[i].resid->ID) << endl; } return 0; }
c ++
Guido polles
source share