user13243126
user13243126

Reputation:

Why does Valgrind report me use of uninitialised value and conditional jump or move depends on uninitialised value?

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.

Upvotes: 0

Views: 116

Answers (1)

Hitokiri
Hitokiri

Reputation: 3699

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};

Upvotes: 1

Related Questions