user2019482
user2019482

Reputation: 21

Function Warnings in C

Hello guys i have threefunctions for which i get 4 warnings...!!

The first one is this

void evaluatearxikos(void)
{
    int mem;
    int i;
    double x[NVARS+1];

    FILE *controlpointsarxika;

    controlpointsarxika = fopen("controlpointsarxika.txt","r");
    remove("save.txt");


    for(mem = 0; mem < POPSIZE; mem++)
    {
        for(i = 0; i < NVARS; i++)
        {
            x[i+1] = population[mem].gene[i];


        }
        rbsplinearxiki();

        XfoilCall();

        population[mem].fitness = FileRead();
        remove("save.txt");


    }

       fclose(controlpointsarxika);
}

For this one the compiler warns me tha variable x is set but not used...!! But actually i am using the variable x...!!!

The second function is this one...

void elitist(void)
{
    int i;
    double best,worst;
    int best_mem,worst_mem;

    best = population[0].fitness;
    worst = population[0].fitness;

    for(i = 0; i < POPSIZE - 1; i++)
    {
        if(population[i].fitness > population[i+1].fitness)
        {
            if(population[i].fitness >= best)
            {
                best = population[i].fitness;
                best_mem = i;
            }

            if(population[i+1].fitness <= worst)
            {
                worst = population[i+1].fitness;
                worst_mem = i+1;
            }
        }

        else
        {
            if(population[i].fitness <= worst)
            {
                worst = population[i].fitness;
                worst_mem = i;
            }

            if(population[i+1].fitness >= best)
            {
                best = population[i+1].fitness;
                best_mem = i+1;
            }
        }
    }

    if(best >= population[POPSIZE].fitness)
    {
        for(i = 0; i < NVARS; i++)
        {
            population[POPSIZE].gene[i] = population[best_mem].gene[i];


        }

        population[POPSIZE].fitness = population[best_mem].fitness;
    }

    else
    {
        for(i = 0; i < NVARS; i++)
        {
            population[worst_mem].gene[i] = population[POPSIZE].gene[i];

        }

        population[worst_mem].fitness = population[POPSIZE].fitness;
    }
}

For this one i get two warnings that the variables worst_mem and best_mem may be used uninitialized in this function..!! But i initialize values to both of them..!!

And the third function is this...

void crossover(void)
{
    int mem,one;
    int first = 0;
    double x;

    for(mem =0; mem < POPSIZE; mem++)
    {
        x = rand()%1000/1000;

        if(x < PXOVER)
        {
            first++;

            if(first%2 == 0)
            {
                random_Xover(one,mem);
            }

            else
            {
                one = mem;
            }
        }
    }
}

For which i get that the variable one may be used unitialized..!! But it is initialized..!

Can you please tell me what is wrong with these functions...??

Thank you in advance

Upvotes: 1

Views: 132

Answers (3)

Jite
Jite

Reputation: 4368

First: You set x but do not use it. It's a local variable that gets set but it's dropped as soon as the function returns.

Second: There might be values that makes it so that your best_mem/worst_mem never gets set in your if/else, but you are using them later on. If they haven't been set, they contain garbage if not initialized.

Third: While it shouldn't happen that you try to use an uninitialized variable in your code, it still looks weird and compiler doesn't see that it won't happen first time.

When you get compiler warnings, treat is as you are doing something wrong or rather not recommended and that it could be done in a better way.

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 60037

  1. The x variable is only used on the left hand side (i.e. assigned a value). You are not using that value on the right hand side or pass it to a function.
  2. It may be possible to get to the end of the loop for(i = 0; i < POPSIZE - 1; i++) without those variables given a value. Why not set them in the declaration.
  3. The call to random_Xover(one,mem); could be called when one is not set. Change the line int mem,one; to int mem,one = <some value>;

Upvotes: 0

mah
mah

Reputation: 39847

In your first function, you set (assign) x, but you never read it, hence you are not using it... you're only wasting CPU cycles by writing to it. (Note also that because you index it as i+1 you write beyond the space you've allocated for it).

In the second function, your initializations to those variables are in conditional blocks. You can see that (perhaps? I didn't verify) in all conditions they are initialized but your compiler isn't that smart.

In your third function, it does appear that one could be refered to without having first been initialized.

Upvotes: 4

Related Questions