#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.Axis | Compilers | Result |
ArchLinux 64 | clang 2.9, gcc 4.5.2 | 0 |
Win7 32 | Visual C ++ 2005, 2008, 2010 | 1 |
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). 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 functionThe 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:auto
) variable will usually be deterministic even on systems without zeroing free memory?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. 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