创建动态总和直至 eof 时的核心转储

问题描述 投票:0回答:1

以下代码在某处存在缺陷,当输入 11/12/13 数字时,我收到“中止(核心转储)”警报。怎么了? #include <stdio.h> #include <stdlib.h> int main(){ // sum nums until EOF int* array; // pointer to the first element int size = 10; // memory for 10 elements int i = 0, sum = 0; array = malloc(sizeof(int)*10); //get memory while(scanf("%d",(array + i))!= EOF){ //read until EOF //add element to the sum sum += *(array+i); //realloc memory if(i == size-1){ size *= 2; array = realloc(array, size); } i++; } free(array); //free the memory printf("%d\n\n",sum); //print sum return 0; }


c dynamic-memory-allocation coredump
1个回答
0
投票

© www.soinside.com 2019 - 2024. All rights reserved.