Thread: Memory allocation error (Nicely formatted this time)

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Memory allocation error (Nicely formatted this time)

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Too late - fixed and answered already.
    http://cboard.cprogramming.com/showthread.php?t=90629
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Help! -Linked Lists, Memory Allocation, Time Steps, Debugg
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-14-2009, 08:50 PM
  3. Increasing memory allocation in function
    By Ramses800 in forum C Programming
    Replies: 3
    Last Post: 12-16-2008, 05:30 AM
  4. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM