Bug 113224 - Warning "is used uninitialized" raised for an initialized variable
Summary: Warning "is used uninitialized" raised for an initialized variable
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 13.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2024-01-03 20:11 UTC by gandalf
Modified: 2024-01-03 20:43 UTC (History)
0 users

See Also:
Host:
Target: x86_64-pc-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description gandalf 2024-01-03 20:11:42 UTC
In an attempt to convert a float value bytewise to an integer, this function returns the following unexpected GCC warnings at optimization level -O2 and -O3 on x86_64:

unsigned int myfunc() {
  float _f=2.5;
  unsigned int *_x=(unsigned int *)&_f;
  return *_x;
}

gcc -O2 -Wall test12.c -c

test12.c: In function 'myfunc':
test12.c:4:10: warning: '_f' is used uninitialized
test12.c:2:9: note: '_f' declared here

It compiles correctly, at least. The warning goes away when -fno-strict-aliasing is applied.
Comment 1 Andrew Pinski 2024-01-03 20:39:52 UTC
This code is undefined for violating C/C++ aliasing rules.
The warning is because of the undefinedness of the code.
Comment 2 Andrew Pinski 2024-01-03 20:42:42 UTC
>In an attempt to convert a float value bytewise to an integer,

You should use memcpy instead (or an union which itself a GCC extension).
Comment 3 gandalf 2024-01-03 20:43:32 UTC
Thank you for the suggestion. I'll try the union.