Upload
pascari-pavel
View
54
Download
2
Embed Size (px)
DESCRIPTION
Secrets of C, how to cheat through it.
Citation preview
Some dark corners of C-std=c99 -pedantic -Wall -Warn-me-harder
Stolen from Rob Kendrick <rjek+dark [email protected]>
Rob Kendrick <[email protected]>
Dark corner of C
Here it is!
Shadowy escape
Returns 3840 Returns 42
Bizarre keyword reusevoid foo(int x[static 10]);
Passed array must be at least 10 entries.
void foo(char x[static 1]);
Free compile-time non-NULL check!
Warning: Don't breathe this
Yes, that's right, it makes demons fly out of your nose!
(On my system, 1,179,602,721.)
If you do this, I will find you.
Yes, single quotes.
[ ] is just + in disguise
Pointer aliasing
Compiler doesn't
know that *z won't also be *x, so loads it twice :(
Pointer aliasing: Fixed?
Only one load, at the
cost of beauty.
Pointer aliasing: Fixed!
'restrict' means we
promise not to mess about.
Counting up ...
... vs counting down
const confusion
"Constant" integer foo with value 10.And a "constant" pointer to it?
Oops. binds left. But if there's nothing to its left, it binds to the right.
Munchy munchy.
The C specification says that when there is such an ambiguity, munch as much as possible. (The "greedy lexer rule".)
Munchy munchy. Again.
Alas, not
But
Parser go boom.
C keywordsauto break case char const continue default do
double else enum extern float for goto if int long register restrict return short signed sizeof static struct switch typedef union unsigned void
volatile while
Wait, what? is a bizarreness left over from B. The only place it's valid, it's also the default. And that's to say a
variable should be automatically managed, ie, placed on the stack. is its antonym. Ish.
Smallest C programWhat's the smallest C program that will compile and link?
(Don't run this.)
Alas it produces a warning. So we can do this:
Global variables are filthyfoo1.c:
foo2.c:
Global variables are filthyfoo1.c:
foo2.c:
Global variables are filthyfoo1.c:
foo2.c:
// comments are evil
C89: 5 C++: 10 C99: 10
Portable lossless floats
Ruin somebody's dayWhat would you sneak into somebody's headers to drive them mad?
No valid C program will fail to compile with these.
Recommended reads
Sadly very few modern books on the subject of C :(
Thanks for ideas/input...David Thomas
Daniel SilverstoneClive Jones
Peter Van Der LindenErlend Hamberg