This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/24157] New: g++ allows variable to be initialized by value of variable being initialized
- From: "gahs at phys dot ksu dot edu" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 1 Oct 2005 06:58:13 -0000
- Subject: [Bug c++/24157] New: g++ allows variable to be initialized by value of variable being initialized
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
I found by pure accident that g++ allows the RHS of an initialization to be the
very variable that is being initialized. Two minimalist examples are given
below. The same behavior is present under gcc 2.95.2, 3.3.3, and 3.4.1.
I'm suprised that the new variable is even considered to be defined in the
context of the RHS of its own initializer, although I suppose it is concievable
in this mad mad world that it might be valid syntax under the standard. But
surely the compiler should at least issue the "warning: ... might be used
uninitialized" message when compiled with -O -W -Wall, since the variable is
definitely not initialized yet!
Example 1:
int main()
{
int i; // the compiler will only warn about i being uninitialized
int j= i;
int k= k; // the compiler has no problem with this
return i+j+k; // no warning about k being possibly uninitialized
}
Example 2:
class A {
int x;
};
int main() {
A* aptr= (A*)aptr; // why is this line valid?
return (aptr==0); // aptr isn't _really_ initialized, but no warning...
}
--
Summary: g++ allows variable to be initialized by value of
variable being initialized
Product: gcc
Version: 3.4.1
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: gahs at phys dot ksu dot edu
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24157