Hello,
I am using code::blocks on Windows plateform for my program. In my program, in one of the loop, I am using realloc function to increase the allocation space for one pointer to structure. It almost always fails and program does not run sucessful. For understanding, I tried the following program.
Code:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i, j, loop_end=15;
double x=10.00, *y;
//char x='A', *y;
y=(double*)calloc(1, sizeof(double));
//y=(char*)calloc(1, sizeof(char));
*y=x;
printf("\n\n STARTING: @ %d, value= %lf\n\n",y, *y);
//printf("\n\n STARTING: @ %d, value= %c\n\n",y, *y);
for(i=0; i<=loop_end-1; i++)
{
printf("\n Loop No: %d\n--------------",i);
if((realloc(y, (i+2)*sizeof(double))) == NULL)
//if((realloc(y, (i+2)*sizeof(char))) == NULL)
{
printf("\n\n In loop %d mem-err occured\n",i);
free(y);
getchar();
return 0;
}
for(j=0; j<=i; j++)
printf("\n [%2d] Address: %d Value: %lf",
j, (y+j), *(y+j));
//printf("\n [%2d] Address: %d Value: %c",
//j, (y+j), *(y+j));
*(y+i+1)=*(y+i)+1;
}
printf("\n Loop completed");
free(y);
getchar();
return 0;
}
When I run it for char few loops run (sometimes 5, sometimes 6) and for double it never runs sucessfully even two loops (in compilation, no warning). Please let me know where I am doing some mistake in this ??
Thanks and regards,
Amal.