Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!ulysses!ulysses.att.com!mwb From: mwb@ulysses.att.com (Michael W. Balk) Newsgroups: comp.lang.c Subject: Re: Memory allocation / data access Summary: malloc error Message-ID: <14477@ulysses.att.com> Date: 16 Mar 91 23:26:29 GMT References: <29491@cs.yale.edu> Sender: netnews@ulysses.att.com Distribution: na Lines: 35 In article <29491@cs.yale.edu>, jim@doctor.chem.yale.edu (James F. Blake) writes: > > I have two questions: 1) Have I allocated the storage properly and 2) Is this > the most efficient way to access the data (i.e., monomer[].atom[].x). > I am allocating storage for n-molecules, each with n-atoms and 3 coordinates. > Any information would be greatly appreciated. > > . . . > > if ((monomer = (solvent *) malloc (nmol * sizeof (solvent *)))==NULL) > exit(1); . . . In answer to your first question I see an error in malloc. Instead of sizeof(solvent *), you should have sizeof(solvent), i.e., you want to allocate space for nmol of solvent, not pointers to solvent. Malloc then returns a pointer to the start of this space (type char* or void*), which you have then correctly cast to a pointer to solvent. Michael W. Balk AT&T Bell Laboratories Murray Hill, NJ 07974 mwb@ulysses.att.com