im getting the following message: "error for object 0x3fd6a7ef: pointer being reallocated was not allocated". It's got something to do with the way i am allocating memory to the pointer "acceptors." The strange thing is that even if I allocate the memory statically ( e.g. ACCEPTORS acceptors[50]) I still get an error message.
I have previously defined ACCEPTORS as:
Code:
typedef struct
{
int index;
double da_r;
double theta;
}
ACCEPTORS;
All the user-defined functions within this function work perfectly fine, the problem that I'm having is with memory allocation. Any help and suggestions would be greatly appreciated.
Here's the function:
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);
}
thank you.