-
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
-
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.
-
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
-
> 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.
-
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