Thread: HELP!!! Memory allocation error!

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

    HELP!!! Memory allocation error!

    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.
    Last edited by Salem; 06-08-2007 at 11:09 AM. Reason: It's taggerific!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    PLEASE, USE CODE TAGS around your code!


    Edit:
    I am no expert on malloc, but I don't think you can realloc something that was never malloc'd or calloc'd in the first place.
    Last edited by Adak; 06-08-2007 at 12:10 PM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Might I add that you should pick a topic name that doesn't sound like your nuclear reactor just melted down.... It only serves to make light of the cases that are indeed urgent.

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

    Ignore This Post

    it should be better now, please help.
    Last edited by killiansman; 06-08-2007 at 11:13 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Do you have javascript turned off - because posts with braces in are not normally allowed.
    You're supposed to have gotten a prompt to tell you to use [code][/code] tags.

    Post edited - just this once.
    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > im getting the following message: "error for object 0x3fd6a7ef: pointer being reallocated was not allocated"
    Start with
    ACCEPTORS *acceptors = NULL;
    If you want realloc to behave like malloc on the first call.

    Also, do a board search on the safe way to call realloc, which doesn't leak memory if it fails.


    > for(a=0;a<=num_acc;a++)
    This looks like a buffer overflow. The normal loop is for < N, not <= N
    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.

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

    Sweet!

    thanks a lot Salem, that worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  2. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM