0
我該如何強制Realloc行爲像calloc? 例如:使Realloc的行爲像Calloc
我有以下結構:
typedef struct bucket0{
int hashID;
Registry registry;
}Bucket;
typedef struct table0{
int tSize;
int tElements;
Bucket** content;
}Table;
,我有下面的代碼,以成長表:
int grow(Table* table){
Bucket** tempPtr;
//grow will add 1 to the number available buckets, and double it.
table->tSize++; //add 1
table->tSize *= 2; //double element
if(!table->content){
//table will be generated for the first time
table->content = (Bucket**)(calloc(sizeof(Bucket*), table->tSize));
} else {
//realloc content
tempPtr = (Bucket**)realloc(table->content, sizeof(Bucket)*table->tSize);
if(tempPtr){
table->content = tempPtr;
return 0;
}else{
return 1000;//table could not grow
}
}
}
當我執行它,表正常增長,並且其中的「桶」的大部分被初始化爲NULL ptr。但是,並不是所有的都是。
我該如何讓Realloc的行爲像calloc?當它創建新的「桶」時,它們初始化爲NULL
Call :: memset清除實時存儲器的額外部分? – Mine