As a school assigment I need to create memory block with malloc and when its full it should extend itself with realloc.This will continue in a infnite loop.But I am getting segmantation fault error and I think the mistake is in realloc so can anyone tell me what I am doing wrong ?
#include "stdio.h"
#include "stdlib.h"
int main()
{
int boyut=5;
int* dizi;
dizi=(int *)malloc((boyut*sizeof(int)));
int b=0;
while(b<1)
{
if(*dizi<boyut){
scanf("%d",dizi);
dizi++;
}
else if(boyut=*dizi)
{
printf("boyut yetersiz");
dizi = (int*) realloc (dizi,2*sizeof(int));
boyut=*dizi*2;
}
}
return 0;
}
Edited according to answers its extending the malloc area by *2.
int main()
{
int boyut=2;
int* dizi;
int* dizi1;
int n;
dizi=(int *)malloc((boyut*sizeof(int)));
int b=0;
int i;
while(b<1)
{
if(i<boyut){
for(i=0;i<boyut;i++){
printf("\nDeğer Girin:");
scanf("%d",dizi);
}
}
else if(boyut=i)
{
boyut=boyut*2;
printf("\nBoyut yetersiz yeni boyutu %d yapıyoruz",boyut);
dizi1 = (int*) realloc (dizi,boyut*sizeof(int));
dizi=dizi1;
}
}
return 0;
}
Correct answer thanks for those who have same questions can use it.