0
votes

I need to write a program that reads data from a file that is given by the command line and prints the largest value of a circle area on standard output. The program compiled without any errors under gcc. But Valgrind reports me dozens of errors about "Use of uninitialised value of size 8" and "Conditional jump or move depends on uninitialised value(s)". I suspect it being because of the variable area from the struct CIRCLE but can't figure it out how to resolve the problem.

Struct prototype:

typedef struct { double x, y, r, area; } CIRCLE;

Function prototype for area computing:

int compute_circle_area(CIRCLE * circle, CIRCLE * max_area)

Function code:

circle->area = pow(circle->r, 2) * PI;
max_area->area = pow(max_area->r, 2) * PI;
return circle->area > max_area->area ? 1 : 0;

Main function prototype:

int main(int argument_count, char ** argument_vector)

Function code:

FILE * stream;
CIRCLE circle;
CIRCLE max_area;
if((stream = fopen(* (argument_vector + 1), "r")) != NULL)
{
    while((fscanf(stream, "(%lf, %lf, %lf)\n", &circle.x, &circle.y, &circle.r)) == 3)
    {
        if(++counter == 1) max_area = circle;
        else
        {
            if(compute_circle_area(&circle, &max_area)) max_area = circle;
        }
    }
    fclose(stream);
}
printf("Circle with the largest area: \n");
printf("(%lf, %lf, %lf), Area: %lf", max_area.x, max_area.y, max_area.r, max_area.area);
return 0;

I also have a global static int counter = 0 to manipulate the first loop to set max_area to circle.

1
What line(s) of that causes the warnings?Shawn
And you should provide a minimal reproducible example program, not just a few lines taken from here and there.Shawn
The first time through the loop you do not compute the area of circle. You then assign circle to max_area, which now has an unspecified area field. Given a possible random value for max_area.area one cannot assume the comparison of computed areas will ever give you the desired result.Jim Rogers
@JimRogers Any tip on how to avoid problems like that in the future ?user13243126
@MarkoMajstorovic Compute the area of the first circle before assigning it to max_area, or alternatively compute the area of max_area after assigning the first circle to max_area. Your compute_circle_area function should be refactored into two functions. One to compute the area and another to compare two circles.Jim Rogers

1 Answers

1
votes

You can initialize the value for the struct circle and max_area:

CIRCLE circle = {0,0,0,0};
CIRCLE max_area = {0,0,0,0};