I'm working on linux and using as compiller gcc. I'm make some expriences with the functions malloc and realloc try to understand how it works. But when I execute the program give me segmentation fault. next my code:
#include<stdio.h>
#include<stdlib.h>
 int main(){
  register int cont=1;
  int i,n,*a;
  a=(int*)malloc(sizeof(int));
  scanf("%d",&n);
  while(n!=0){
   if(a!=NULL)
    a=(int*)realloc(a,cont*sizeof(int));
   else 
    goto exit;
   a[i]=n;
   scanf("%d",&n);
   cont++;
   i++;
 }
 for(i=0;i<cont;i++)
  printf("%d\n",a[i]);
 free(a);
 return 0;
 exit: printf("No memory\n");
 free(a);
 return -1;
}
Why this don't work and whats wrong with my code?
 
     
     
    