我有一个 int 数组,当它看到比现有值更高的 int 值时需要增长,有没有办法调用 realloc() 并设置正在创建的内存中的所有新空间?或者我需要循环遍历内存中所有新的空间并将它们一一设置为0?为了清楚起见,下面的代码。
int main(){
int i;
int currentSize = 10;
int *checkList = malloc(sizeof(int) * currentSize);
while((i = readInt(fp))){
if(i > currentSize){
currentSize = i + 1;
checklist = realloc(checkList, sizeof(int) * (i + 1));
//Need to loop through checklist and declare empty mem to 0?
}
if(!checkList[i]) checkList[i]++;
}
//should have an array where seen values index in checklist == 1
}
Run Code Online (Sandbox Code Playgroud)