NO

Recent Posts

Pages: 1 ... 8 9 [10]
91
General discussions / Re: Compiler Warning
« Last post by frankie on March 31, 2022, 05:45:23 pm »
Ok John I see now.
The compiler seems correct, there is a flaw in the logic of the function 'MainDlgProc'.
When you declare:
Code: [Select]
HICON hIcon = NULL; or
Code: [Select]
HICON hIcon;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: [Select]
volatile HICON hIcon = NULL; 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.

92
General discussions / Re: Compiler Warning
« Last post by MrBcx on March 31, 2022, 05:41:51 pm »
This works for me on V11

HICON hIcon = {0};
93
General discussions / Re: Compiler Warning
« Last post by John Z on March 31, 2022, 01:00:31 pm »
Frankie, Thanks for the reminder to include the Pelles C version too.

 Initial work was from version 10.  I'm a slow adopter  :)

However attached is a simple program test case which I ran in version 10 and version 11.  The reported
warnings are somewhat different from each but both versions show the only way to satisfy the compiler (ie no warning)
is to use 'static HICON hIcon;' or 'volatile HICON hIcon = NULL;'

IMO should not be the case because clearly
Code: [Select]
hIcon = LoadImage( ghInstance,MAKEINTRESOURCE(JGZ),IMAGE_ICON,
                           32,32,LR_SHARED);
will affect the value of hIcon.

Just uncomment the variable declare at the top of MainDlgProc and compile.  The warning line number I put in the comments will be off a bit because I added comments while testing.  There are 5 test cases.  Should have been 6
I forgot to include 'static HICON hIcon = NULL;'


John Z

94
General discussions / Re: Compiler Warning
« Last post by frankie on March 31, 2022, 09:33:07 am »
I'll try to generate a simple example. 

John Z
Yes please, because I can't reproduce such error with PellesC V11.
95
General discussions / Re: Compiler Warning
« Last post by John Z on March 31, 2022, 02:40:57 am »
Yes I understand why, but one or the other should not be in error I think.

static HICON hIcon = NULL;

Also 'fixes' (no warnings) it but clearly not that desirable.

I'll try to generate a simple example. 

John Z
96
General discussions / Re: Compiler Warning
« Last post by frankie on March 30, 2022, 03:22:22 pm »
There is something wrong here.
You should get the message:
Code: [Select]
: warning #2154: Unreachable code. because the compiler evaluates that the the condition:
Code: [Select]
hIcon != NULL is never met.
For this reason the whole block:
Code: [Select]
if (hIcon) {DestroyIcon(hIcon);} is removed from compilation output as "dead code".
This could happen if:
  • hicon is a local variable
  • No change happens to its value so the compiler can safely assume that it will always be null.
To understand if these conditions are really met, and this is not a compiler bug, you should post the smallest piece of complete code that shows the problem.

If tis is a bug you can try as workaround:
Code: [Select]
volatile HICON hIcon;The 'volatile' qualifier informs the compiler that the variable can be changed by external causes, and for this reason no assumptions on its value have to be made.
97
General discussions / Compiler Warning
« Last post by John Z on March 30, 2022, 01:35:19 pm »
if you declare
Code: [Select]
HICON hIcon = NULL;
then within
Code: [Select]
case WM_INITDIALOG:
   hIcon = LoadImage( ghInstance,MAKEINTRESOURCE(JGZ),IMAGE_ICON,
                           32,32,LR_SHARED);
                           
then within
Code: [Select]
case WM_CLOSE:
   if (hIcon) {DestroyIcon(hIcon);}
        
The compiler generates a Warning message for > 'if (hIcon) {DestroyIcon(hIcon);}'
: warning #2154: Unreachable code.
 
Then change the declare to
Code: [Select]
HICON hIcon;
The compiler generates a Warning message for > 'HICON hIcon;'
:warning #2229: Local 'hIcon' is potentially used without being initialized.

Is there a third way that has no warning ?  I use warnings Level 2 and C11.

John Z
98
User contributions / Re: vlc example
« Last post by John Z on March 21, 2022, 10:59:10 pm »
Looks interesting, Thanks!

John Z
99
Beginner questions / Re: How to check all strings?
« Last post by AlexN on March 21, 2022, 04:09:16 pm »
You can try this:
Code: [Select]
char *data[] = {"data1", "data2", "data3", "data4"};
count = sizeof(data)/sizeof(data[0]);
or make a macro:
Code: [Select]
#define COUNT(x) (sizeof(x)/sizeof(x[0]))

char *data[] = {"data1", "data2", "data3", "data4"};
count = COUNT(data);
100
Beginner questions / Re: How to check all strings?
« Last post by bitcoin on March 21, 2022, 02:21:16 pm »
TimoVJL , CLR
Thank you !

Excuse me please but why there is NULL in *data[], isn't it an array of strings so there should be no need to NULL terminate it?

Code: [Select]
char *data[] = { "data1", "data2", "data3", "data4", NULL };

Because how I can guess , where is the end of array?
There may be a different number of string. Do not manually enter the count.
Pages: 1 ... 8 9 [10]