Thread: Program Terminating With Error On Exit

  1. #16
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    Code:
    int ReallocateMemory(RECORD ** ptrDatabase, int * currentMemSize) {
    	RECORD * ptrNew;
    
    	ptrNew = (RECORD *) realloc(ptrDatabase, (*currentMemSize + memIncrease) * sizeof(RECORD));
    	if (ptrNew == NULL){
    		return 0;
    	}
    	else{
    		ptrDatabase = &ptrNew;
    		*currentMemSize += memIncrease;
    
    		return 1;
    	}
    }
    I have got my allocateMemory function working using the pointer to a pointer method. However I am having trouble with the reallocate function. I have posted my current code and highlighted the areas which i belive to be incorret (ptrDatabase on the 1st red line and &ptrNew on the 2nd red line.

    Thanks chris

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Change the second one to:
    Code:
    *ptrDatabase = ptrNew;
    It's just like your call to increase the memory. You dereference the pointer to assign it a value. You dereference a pointer to a pointer to assign it a pointer. You dereference a pointer to an integer to assign it an ingeter. See?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    i won't hold my breath but i think, thanks to everyones help here I have managed to get both of my memory functions to work correctly and the program to stop crashing on exit. I'll let you know if i break it again

    Thanks again all

    Chris

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ptrDatabase = &ptrNew;
    *ptrDatabase = ptrNew;

    Oh, and don't cast the result of malloc and realloc, do it like this
    ptrNew = realloc(ptrDatabase, (*currentMemSize + memIncrease) * sizeof(RECORD));
    the FAQ explains why.

  5. #20
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    thanks for the tip. I have made the changes and all is still working fine.

    I have also changed the sizeof(RECORD) to sizeof(**ptrDatabase) as the faq surgested.

    Cheers again

    Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Dialog Box causes program to exit
    By norbs27 in forum Windows Programming
    Replies: 10
    Last Post: 01-14-2008, 10:09 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM