Once again I would like to appologize for posting that previous mess; hopefully this will be a lot easier to read and understand. The problem that I'm having is that I'm getting the following message during runtime "error for object 0x3fd6a7ef: pointer being reallocated was not allocated." I'm trying to dynamically allocate memory using realloc, but the strange thing is that I get even more bizarre results when I allocate memory statically (e.g. ACCEPTORS acceptor[100]). earlier in the program I defined ACCEPTORS as:
Code:
typedef struct
{
int index;
double da_r;
double theta;
}
ACCEPTORS;
Here is the rest of my function (btw, the user-defined functions work perfectly fine):
Code:
double checkforhbonds(MOLECULE molecule1,int i,MOLECULE molecule2)
{
double dhb;
double ijlength,minlength,alength,dhlength,E_hb,jlength,ialength;
int j,minj,donor,atj,num_acc,a;
ACCEPTORS *acceptors;
minlength=100000; //fix this
num_acc=0;
dhb = dveclength(molecule1.atom[i].cart); //magnitude of h atom vector
for(j=0;j<molecule1.numatoms;j++)
{
if(j!=i)
{
jlength=dveclength(molecule1.atom[j].cart); //magnitude of test donor
ijlength=fabs(dhb-jlength); //distance between h-atom and test donor
if(ijlength<minlength)
{
minlength=ijlength;
donor=j;
}
}
}
dhlength=dhb-minlength;//length betwen h atom and donor
for(atj=0,num_acc=0;atj<molecule2.numatoms;atj++)
{
alength=dveclength(molecule2.atom[atj].cart);//magnitude of test acceptor vector
ialength=fabs(dveclength(molecule1.atom[donor].cart)-alength); //distance between donor and prospective acceptor
if((ialength)<HBONDTHRESHOLD)
{
acceptors=realloc(acceptors,(num_acc+1)*sizeof(ACCEPTORS));
dhlength=dhb-minlength;
acceptors[num_acc].index=atj;//index number in molecule of acceptor
acceptors[num_acc].da_r=ialength; //Rda difference between acceptor and h difference between donor and h
acceptors[num_acc].theta=dvecangle(dvecsubtr(molecule2.atom[atj].cart,molecule1.atom[i].cart),dvecsubtr(molecule1.atom[donor].cart,molecule1.atom[i].cart)); //theta
num_acc++;
}
}
if(num_acc>0)
{
for(a=0;a<=num_acc;a++)
{
E_hb=E_hb+Ehbonds(acceptors[a].da_r,acceptors[a].theta);
}
}
free(acceptors);
return(E_hb);
}
any help would be very appreciated.