Pelles C > General discussions

Compiler Warning

<< < (2/2)

MrBcx:
This works for me on V11

HICON hIcon = {0};

frankie:
Ok John I see now.
The compiler seems correct, there is a flaw in the logic of the function 'MainDlgProc'.
When you declare:
--- Code: ---HICON hIcon = NULL;
--- End code ---
or
--- Code: ---HICON hIcon;
--- End code ---
You are creating an automatic variable that will be lost as soon as execution flow exits the function. This imply that the value of the variable 'hicon' exists for a single switch case per call.
So you can have:
* When 'uMsg' is 'WM_INITDIALOG' you are assigning a value to it, that will be lost on return.
* When 'uMsg' is 'WM_CLOSE':

* If 'hicon' isn't initialized you're using a dangling value for the 'if' test (warning #2116)
* if 'hicon' is initialized to NULL the comparison is constant and always false and the conditional code never executed ('warning #2154: Unreachable code' and 'warning #2154: Unreachable code')The problem is essentially related to the scope of the variable. Probably this piece of code was originally positioned in the 'WinMain' function and its scope was persistent for the whole program execution ('WinMain' scope end when program ends). Moving the code to a function the scope was modified and local to the function itself.
Making it 'static' guarantee that the variable exists and retain its value between calls, miming the functionality as when declared in the 'WinMain'.

Anyway something that I've not fully understand happens using the 'volatile' qualifier.
Using:
--- Code: ---volatile HICON hIcon = NULL;
--- End code ---
the compiler doesn't complain, while omitting the initialization to 'NULL' V11 warns about constant compare. What's the rationale behind this?
Anyway making it 'static' no more complains are made.

John Z:
Thanks frankie,

The code didn't come from anywhere other than my mind. ..... ;)

I guess it is my misunderstanding coming from a C procedural flow world to a Windows event driven world.
I thought I could use WM_INITDIALOG to set the hIcon once and it would be there for the duration of the existence
of the dialog, but I knew it needed to be destroyed before closing the dialog.  I had ended up using Static in the code but thought, as I was trying to find a bug, that this might be my bug source.  So since the only dumb question is the one you don't ask, I figured I'd better check.   :)

Appreciate the help,
John

frankie:
John you're always welcome.
It is good IMHO to talk and discuss some detail that could be interesting for many, also considering that more skilled is the programmer more often she/he stops at very simple problems because of the mind orientation that looks always for major issues...  ;D
It is the human nature indeed at last...  :(
On my side I still have doubts about the compiler warnings when the 'volatile' qualifier is used, as I reported in my previous post.

Grincheux:

--- Quote ---[size=0px]The compiler generates a Warning message for > 'if (hIcon) {DestroyIcon(hIcon);}'[/size][/size][size=0px]: warning #2154: Unreachable code.[/size]
--- End quote ---
[size=0px]

Yes because it is declared as NULL

If hIcon is local this is normal[/size]

Navigation

[0] Message Index

[*] Previous page

Go to full version