Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 260 Vote(s) - 3.65 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Initializing variable with itself: how come it is not a compile-time error?

#1
I am a bit baffled that I managed to accidentally write code equivalent to this one

int a=a; // there is not a declared before this line

and the compiler happily compiled it - both gcc and clang, which are very standard-conforming and have good diagnostics. (With `-Wall`, gcc 4.8 warns about uninitialized variable; clang does not).

I thought the RHS of the assignment will be evaluated prior do LHS, hence causing `a` to be undefined on the RHS. Can I have some simple clarification about why is this syntactically legal?

Reply

#2
It will be a compile-time error if you tell GCC to make it so:

gcc -Winit-self -Werror

Note that sadly this diagnostic is not enabled by most of the usual suspects like -Wall.
Reply

#3
The scope of a name starts right after its complete declaration. Thus, `a` is already in scope at the time of its initialization.

Stroustrup has the very same example as you noted to demonstrate scoping rules.

See: Stroustrup "The C++ Programming Language, 4th ed.", p.158.

Edit:
Oops, didn't notice question was tagged with "C", but still, I think it works for C quite as well
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through