Memory allocation / data access

Michael W. Balk mwb at ulysses.att.com
Sun Mar 17 09:26:29 AEST 1991


In article <29491 at cs.yale.edu>, jim at 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 at ulysses.att.com



More information about the Comp.lang.c mailing list