我想在达到最大容量时调整数组的大小。但是在我这样做之后出现了错误。/a.out 请帮助我...
Error: a.out: malloc.c:3574: mremap_chunk: Assertion `((size + offset) & (mp_.pagesize-1)) == 0' failed.
代码:
#include<stdio.h>
#include <stdlib.h>
int main(void)
{
int cap=5;
int *arr = malloc(cap*sizeof(int));
FILE *f;
if((f=fopen("/home/file.txt","r"))==NULL)
printf("You cannot open");
while(fscanf(f, "%d", arr++)!=EOF)
{
index++;
if(index==cap-1)
arr = realloc(arr, (cap +=1) * sizeof(int));
}
return 0;
}