NO

Author Topic: Problem with variables which are initialized but never used  (Read 3357 times)

skirby

  • Guest
Problem with variables which are initialized but never used
« on: August 01, 2006, 04:17:52 PM »
Hello,

I don't know if it is really a bug but I did not know where to post my problem.

Here is a simple example:
Code: [Select]
#include <stdio.h>

void test(void)
{
int i, j, a;

  printf("Begin");

i = 1;
j = 8;
//  for (a = 0; a < 10; a++);
a = 4;

  _asm{
Boucle:
    MOV eax, DWORD PTR [a]
    DEC DWORD PTR [a]
    JNZ Boucle
  }
}

int main(void)
{
  test();
return 0;
}

When I compile this example, I have the following warning:
Quote
Building main.obj.
D:\Utils\PellesC\Projects\Test\main.c(5): warning #2115: Local 'i' is initialized but never used.
D:\Utils\PellesC\Projects\Test\main.c(5): warning #2115: Local 'j' is initialized but never used.
D:\Utils\PellesC\Projects\Test\main.c(5): warning #2115: Local 'a' is initialized but never used.
Building Test.exe.
Done.

I already seen a similar message on the forum (with the function search) but my problem is a little bit different (see http://smorgasbordet.com/phpBB2/viewtopic.php?t=830).

I know my example is rather stupid but I would like to understand why Pelles C does not initialize the variables which are declared initialzed correcly.

In fact, when you debug this piece of code, you can see that i, j and a variables are not initialized correctly with values defined in the code.
For example, the variable a is not equal to 4 but it has an unknow value.

Moreover, if you debug step by step the code, you could see Pelles C step over the line a = 4;  :shock:
It simply ignore it.

Now, uncomment the line with for instruction and debug the piece of code.
The variable a is correctly initialized.

Is it normal?

Thanks and have a nice day.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Problem with variables which are initialized but never used
« Reply #1 on: August 01, 2006, 05:23:19 PM »
Well probably there is some orthodoxy in the way the compiler behaves.
I suppose that if you try to initialize a variable, but don't use it, and you have the optimizations on, the compiler spares the initialization code. Is an optimization: why initialize (or create) a variable if you don't care about it?.
In the case of variable 'a', that is used, it seems a bug. It seems that the compiler optimizer ignores the assembly code (infact if you uncomment the for loop the C code signals that the variable is used and the initialized code is created).
As a workaround try using the 'volatile' qualifier:
Code: [Select]
 int volatile a;
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

skirby

  • Guest
Problem with variables which are initialized but never used
« Reply #2 on: August 01, 2006, 05:55:29 PM »
You are great frankie !!!

If I modify my code from int a; to int volatile a; it works very well  :mrgreen:
The variable is now correctly initialized.  

Thanks and have a nice day.
 =D>