C
Curley Q.
for(;
{
c = a * b;
c %= 100000;
printf("%9d", c);
d = int2str(c);
left = midstr(d, 0, 3);
right = midstr(d, strlen(d)-3, 3);
free(d);
a = atoi(left);
b = atoi(right);
if(a * b == 0)
break;
printf("%6d %6d\n", a, b);
}
free(left);
free(right);
In the code fragment above d is a buffer allocated by a call to
malloc in int2str and freed each pass through the loop. left and
right are also buffers allocated by calls to malloc in midstr,
but they are freed only once when execution breaks out of the
loop. What happens to the memory that is allocated to left and
right in each call to midstr? Is allocated memory getting cleaned
up properly in this code?
{
c = a * b;
c %= 100000;
printf("%9d", c);
d = int2str(c);
left = midstr(d, 0, 3);
right = midstr(d, strlen(d)-3, 3);
free(d);
a = atoi(left);
b = atoi(right);
if(a * b == 0)
break;
printf("%6d %6d\n", a, b);
}
free(left);
free(right);
In the code fragment above d is a buffer allocated by a call to
malloc in int2str and freed each pass through the loop. left and
right are also buffers allocated by calls to malloc in midstr,
but they are freed only once when execution breaks out of the
loop. What happens to the memory that is allocated to left and
right in each call to midstr? Is allocated memory getting cleaned
up properly in this code?