PDA

View Full Version : calloc & realloc



Rob The Bloke
08-06-2000, 05:17 AM
Here's my problem:

I'm creating some arrays using the calloc function.

At some point later in my program I may need to increase the size of these arrays.

I'm wandering if it is possible to reallocate memory for arrays created with calloc, with realloc ?

It says you can in my C programming book, but it gives no examples of how to do it.

Any help would be much appreciated.

XBCT
08-06-2000, 06:28 AM
Hi!
You can use realloc with calloc just as with malloc....
I think it goes like this:

char * test;

test=(char*)malloc(10);

test=(char*)realloc(test,20)
//Not sure if the realloc callīs syntax is correct look it up in the help....


However realloc is a bitch....Using it has several dangers....The biggest problem is that there is no guarantee that the new allocated space starts at the same memory-address than the old one....Which means that all pointers that are assigned to the array in previous code need to be reassigned after realloc in case the memory location has changed(very likely)....If you donīt reassign them you can get some serios crash....

Greets,XBTC!

[This message has been edited by XBCT (edited 08-06-2000).]