📜 ⬆️ ⬇️

Such an elementary C / C ++: can-> is

Question for five: what this simple program will print:
#include <stdio.h> typedef int a; ab = 5; int main() { a(b); printf("%d\n", b); return 0; } 
Having already suffered from their favorite language, but have not yet gone through all the gcc bison, they will feel the catch - and rightly so. Hint number zero: it eats C ++, but simple C does not choke.

Tip one. Here is what it prints:
AxisCompilersResult
ArchLinux 64clang 2.9, gcc 4.5.20
Win7 32Visual C ++ 2005, 2008, 20101
but now the question is different: what is a(b) ? After all, I turned the keys: both C ++ and C are all the same. More precisely: clang and gcc print one (0), and visuals another (1).

It should already doperet. Not? Tip two: remove the noise, and expand the type:
  int(b); 
Hint two ++ . The introduction of a cast can easily stand to the left of the equal, but now Microsoft will also give zero:
  int(b) = 0; 
People with brains sharpened under 45 o didn’t even begin to think about it - they stored the code in http://ideone.com or compiled it directly from the buffer - do we know xsel on xsel ? The result of this mechanical work with the included -Wall is a hint three :
    [aviaconstructor @ arch64 tmp] # xsel |  gcc -xc ++ -Wall -
    <stdin>: In function 'int main ()':
    <stdin>: 9:25: warning: 'b' is used uninitialized in this function
The program is simple, what a C ++ 11! But if you started right away from the books of Alexandrescu and shy away from the holy simplicity of C, I fill the gap. More precisely, I put a single space between the type and the bracket. For the most unsightly, hint four (five, if in a row):
  int (b); 
At this point in our narrative to the stump, it is clear that a(b) or int(b) is the declaration of a local variable, the same as int b . With her breast, she closes a global variable with the same name. Already in the comments to this post, we can gossip and debunk on the topic:By the way, move the variable on the stack, if interested. Of course, there are not only zeros and ones. And here and now we will go further.

In general, if it lit up to the offensive late, then faith in humanity can be restored with a good word, because about C and C ++ constructions it is said that everything that can be a definition, a definition is. Once again: a(b) looks like a type conversion in C ++, but it looks more like a variable declaration. So the solution is not only the problem, but also the title of our post: the arrow there is a sign of following.
')
All this would be an unclean water curiosity, when we, the smart, crawled along the code well, only very smart bugs. Here are the classics:
  std::string a("my string"); std::string b(); 
Two lines and one empty? Fi-ha! The second is the definition of the parameterless function b , which returns a string. Swears when used, thank God! Much worse when they wanted as always:
  QMutexLocker lock(&globalLock); 
but it happened:
  void MyClass::MyFunc() { QMutexLocker lock(); // ? ThisFuncShallBeProtectedByGlobalLock(); ++objectVarCounter; } 
Even warnings do not wait - just think, the declaration of an unused function! And it will work, that's just without loka (Shishkov, forgive me for continuous anglicisms). In the extreme example, the variable declaration was confused with the function declaration, but a completely different joint:
  { QMutexLocker(&globalLock); //  ? ... 
Do I need to explain why this locker will only work for itself? And I also have a fairy tale: the mouse ran, the tail twisted - Bdams - and instead of equal, I trampled a semicolon in the wrong place:
  MyEnum myEnumMask ; MyEnum(i); 
No, this example is far-fetched! Curved tails and not something written out - still works! Yes Yes. I agree. I agree with everything. But my favorite language is my enemy. Be careful!

Source: https://habr.com/ru/post/117596/


All Articles