realloc重新分配不了内存
#include <stdio.h>#include <stdlib.h>
int main()
{
int current_element = 0;
int total_element = 8;
int * dynamic = (int *)malloc(total_element);
int i;
for(i=0;i<16;i++)
{
if(current_element == total_element)
{
total_element *= 2;
dynamic = (int *)realloc(dynamic,total_element);//在此行貌似出现问题啊,求大虾指教
if(dynamic == NULL)
{
printf("Could not expand!");
exit(1);
}
}
dynamic[current_element++] = i;
printf("%d ",dynamic[i]);
}
free(dynamic);
return 0;
}