Memory allocation / data access

James F. Blake jim at doctor.chem.yale.edu
Sat Mar 16 02:42:53 AEST 1991


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.

  Jim

typedef struct {
  double          x;
  double          y;
  double          z;
} cartesian;

typedef struct {
  cartesian       *atom;
} solvent;

solvent        *monomer;

/* Allocate the necessary storage */

if ((monomer = (solvent *) malloc (nmol * sizeof (solvent *)))==NULL)
  exit(1);

for (i = 0; i < nmol; i++) 
  if ((monomer[i].atom = (cartesian *) calloc (natoms,sizeof(cartesian)))==NULL)
    exit(1);

/* check components */

for (k = 0; k < natoms; k++) {
  for (l = 0; l < natoms; l++) {
    xdis = fabs (monomer[i].atom[k].x - monomer[j].atom[l].x);
    ydis = fabs (monomer[i].atom[k].y - monomer[j].atom[l].y);
    zdis = fabs (monomer[i].atom[k].z - monomer[j].atom[l].z);
  }
}



More information about the Comp.lang.c mailing list