小编Geo*_*oll的帖子

将int数组在内存中的新空间重新分配为0

我有一个 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)

c arrays pointers realloc

0
推荐指数
1
解决办法
901
查看次数

标签 统计

arrays ×1

c ×1

pointers ×1

realloc ×1