Program Terminating With Error On Exit

This is a discussion on Program Terminating With Error On Exit within the C Programming forums, part of the General Programming Boards category; Code: int ReallocateMemory(RECORD ** ptrDatabase, int * currentMemSize) { RECORD * ptrNew; ptrNew = (RECORD *) realloc(ptrDatabase, (*currentMemSize + memIncrease) ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > 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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Dialog Box causes program to exit
    By norbs27 in forum Windows Programming
    Replies: 10
    Last Post: 01-14-2008, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21