Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!male!matilda!harry From: harry@matilda.uk.sun.com (Harry Protoolis - Sun EHQ) Newsgroups: comp.lang.c Subject: Re: Memory allocation / data access Message-ID: <5654@male.EBay.Sun.COM> Date: 15 Mar 91 17:47:33 GMT References: <29491@cs.yale.edu> Sender: news@male.EBay.Sun.COM Reply-To: harry.protoolis@uk.1.com Distribution: na Organization: Sun Microsystems Europe Inc Lines: 65 Jim, You might be better of using pointers instead of constantly dereferencing array elements. cartesian *a_atom, b_atom; for (a_atom = monomer[i].atom, k = 0; k < natoms; k++, a_atom++) { for (b_atom = monomer[j].atom, l = 0; l < natoms; l++, b_atom++) { xdis = a_atom->x - b_atom->x; ydis = a_atom->y - b_atom->y; zdis = a_atom->z - b_atom->z; } } Your allocation looks alright to me, though if you're allocating largish arrays on a smallish system (e.g. > 64K on a PC) you might be better off using a linked list. Oh, and why do you use malloc the first time and calloc the next ?, as they are both array allocations you should, strictly speaking, be using calloc for both. if ((monomer = (solvent *) calloc (nmol, sizeof (solvent *)))==NULL) Regards Harry |> 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); |> } |> } -- (smart Internet mailers) harry.protoolis@uk.sun.com (smart JANET mailers) harry.protoolis@sun-microsystems.co.uk (dumb uucp mailers) ...!sun!sunuk!harry.protoolis 'When I give food to the poor they call me a saint. When I ask why the poor have no food they call me a communist.' - Dom Helder Camara