创建动态总和时的核心转储直到eof
Core dump when creating a dynamic sum until eof
以下代码某处存在缺陷,当输入 11/12/13 号码时,我收到 Aborted(core dumped) 警报。怎么了?
#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;
}
已修复! realloc 应该有 size*sizeof(int).
以下代码某处存在缺陷,当输入 11/12/13 号码时,我收到 Aborted(core dumped) 警报。怎么了?
#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;
}
已修复! realloc 应该有 size*sizeof(int).