Never written something like this, but briefly looking at some code in openbabel for reading molecules, you should do
atom = obMol.NewAtom(); At the beginning of each loop. Your code looks like it is just due referencing an unset pointer, so no surprise it segfaults. You maybe also have to do obMol.ReserveAtoms(molecule.size); -David > On May 7, 2014, at 7:05 PM, Nicolas Cheron <nicolas.cheron.bou...@gmail.com> > wrote: > > Dear all, > > I am working with a program (in C++) which has its own way of dealing > with atoms and molecules. I would like to use the OpenBabel library to > assign types to atoms. For now, when I need to do it I write a .xyz > file and then read it in an OBMol object and assign the types. > > This works, but I am not happy because it could be more efficient. > Thus, I would like to create the OBMol object by hand. I was thinking > of something such as: > > struct Atom{ > float x; > float y; > float z; > char Type[4]; // Element name (H, C, N, O...). > char PotentialType[3]; // Atom type for the scoring function. > }; > > struct Molecule{ > Atom atom[150]; > int size; > }; > > ///////////// > > OpenBabel::OBMol obMol; > OpenBabel::OBAtom *atom; > for (int i=0; i<molecule.size ; i++ ) { > atom->SetVector(molecule.atom[i].x, molecule.atom[i].y, > molecule.atom[i].z); > atom->SetType(molecule.atom[i].Type); > obMol.InsertAtom(*atom); > } > obMol.ConnectTheDots(); > > When I am doing this the code compiles but crashes (Segmentation > fault) and I have not been able to find what is going wrong. I have > also tried with SetVector(const vector3 & v ) but it is the same. > > > Does anyone have a clue of how I can create this object atom by atom? > > Thank you > > Nicolas > > ------------------------------------------------------------------------------ > Is your legacy SCM system holding you back? Join Perforce May 7 to find out: > • 3 signs your SCM is hindering your productivity > • Requirements for releasing software faster > • Expert tips and advice for migrating your SCM now > http://p.sf.net/sfu/perforce > _______________________________________________ > OpenBabel-discuss mailing list > OpenBabel-discuss@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/openbabel-discuss ------------------------------------------------------------------------------ Is your legacy SCM system holding you back? Join Perforce May 7 to find out: • 3 signs your SCM is hindering your productivity • Requirements for releasing software faster • Expert tips and advice for migrating your SCM now http://p.sf.net/sfu/perforce _______________________________________________ OpenBabel-discuss mailing list OpenBabel-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbabel-discuss