Hello for the secondthird time, Reddit. This was a 10-minute lightning talk. It is a presentation, not a book. I spoke when presenting it. Not everything that was said is on these slides, as that would have made for a boring talk. Yes, there are counter examples to some of the things mentioned, and they were discussed when it was presented. And it's meant to be amusing, not dry. Get a grip on yourselves. Lots of love, rjek. P.S. If you want to share this with somebody you know, just give them the URL you received. Do not click "share", as I get an email every time asking to grant permission. These now just get binned automatically so I don't feel the need to smite the universe. P.P.S. Yes, this is in Comic Sans just to troll you.

Some dark corners of C -std=c99 -pedantic -Wall -Warn-me-harder Rob Kendrick

Stolen from Rob Kendrick

Dark corner of C Here it is!

Shadowy escape int x = 42; int func() { int x = 3840; { return x; } } Returns 3840

int x = 42; int func() { int x = 3840; { extern int x; return x; } } Returns 42

Bizarre keyword reuse void foo(int x[static 10]); Passed array must be at least 10 entries. foo.c:8:2: warning: array argument is too small; contains 5 elements, callee requires at least 10 [-Warray-bounds]

void foo(char x[static 1]); Free compile-time non-NULL check! foo.c:8:2: warning: null passed to a callee which requires a non-null argument [-Wnonnull]

Warning: Don't breathe this int x = 'FOO!';

Yes, single quotes.

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.

[ ] is just + in disguise assert(spong[x] == x[spong]); assert(spong[2] == 2[spong]); d = "0123456789abcdef"[n]; d = n["0123456789abcdef"];

Pointer aliasing void foo1(int *x, int *y, int *z) { *x += *z; Compiler *y += *z; doesn't } know that *z movl (%rdx), %eax won't also be addl %eax, (%rdi) *x, so loads movl (%rdx), %eax it twice :( addl %eax, (%rsi) ret

Pointer aliasing: Fixed? void foo2(int *x, int *y, int *z) { int w = *z; *x += w; *y += w; Only one } load, at the movl (%rdx), %eax cost of addl %eax, (%rdi) beauty. addl %eax, (%rsi) ret

Pointer aliasing: Fixed! void foo3(int *x, int *y, int * restrict z) { *x += *z; *y += *z; 'restrict' } means we movl (%rdx), %eax promise not addl %eax, (%rdi) to mess addl %eax, (%rsi) about. ret

Counting up ... int fact1(int n) { int i, fact = 1; for (i = 1; i <= n; i++) fact *= i; return fact; }

fact1: movl testl jle xorl .loop: incl imull cmpl jne .exit ret

$1, %eax %edi, %edi .exit %ecx, %ecx %ecx %ecx, %eax %ecx, %edi .loop

... vs counting down int fact2(int n) { int fact = 1; if (n == 0) return fact; do fact *= n; while (--n != 0); return fact; }

fact2: testl movl je .loop: imull subl jne .exit: ret

%edi, %edi $1, %eax .exit %edi, %eax $1, %edi .loop

const confusion const int foo = 10; const int *foop = &foo; "Constant" integer foo with value 10. And a "constant" pointer to it? int i = 20; foop = &i; Oops. const binds left. But if there's nothing to its left, it binds to the right.

Munchy munchy. z = y+++x; The C specification says that when there is such an ambiguity, munch as much as possible. (The "greedy lexer rule".) z = y++ + x;

Munchy munchy. Again. z = y+++++x; Alas, not z = y++ + ++x; But z = y++ ++ +x; Parser go boom.

C keywords auto 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? auto 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. static is its antonym. Ish.

Smallest C program What's the smallest C program that will compile and link? main; Alas it produces a warning. So we can do this: int main; (Don't run this.)

Global variables are filthy foo1.c: int x; foo2.c: int x; int main() { printf("%d\n", x); } $ gcc -o foo foo1.c foo2.c $ echo $? 0

Global variables are filthy foo1.c: int x = 42; foo2.c: int x; int main() { printf("%d\n", x); } $ gcc -o foo foo1.c foo2.c $ echo $? 0

Global variables are filthy foo1.c: int x = 0; foo2.c: int x = 0; int main() { printf("%d\n", x); } $ gcc -o foo foo1.c foo2.c /tmp/ccx4aT9m.o:(.bss+0x0): multiple definition of `x'

// comments are evil int foo() { int a = 10, b = 2; return a //* //*/ b ; } C89: 5

C++: 10

C99: 10

Portable lossless floats printf("%a\n", 1.2345); 0x1.3c083126e978dp+0 printf("%f\n",atof("0x1.3c083126e978dp +0")); 1.234500

Ruin somebody's day What would you sneak into somebody's headers to drive them mad? #define struct union #define else

Recommended reads

Sadly very few modern books on the subject of C :(

Thanks for ideas/input... David Thomas Daniel Silverstone Clive Jones Peter Van Der Linden Erlend Hamberg

Hello for the secondthird time, Reddit. This was a 10 ...

counter examples to some of the things mentioned, and they were discussed when it was presented. .... /tmp/ccx4aT9m.o:(.bss+0x0): multiple definition of `x' ...

998KB Sizes 1 Downloads 90 Views

Recommend Documents

Hello World This is just a sample PDF. - GitHub
Page 1. Hello World. This is just a sample PDF.

This article was originally published in a journal ...
Hence a second analysis was performed in which only the six largest factors .... A CFA was conducted, using the LISREL 8.54 software (Jo¨reskog & So¨rbom, ...

This article was originally published in a journal ...
Jun 30, 2006 - quantitatively compared to simulated M-mode images, showing a fairly good agreement. © 2006 Elsevier B.V. All ... Blood pressure and velocity map have been obtained by a finite element ... per resolution cell. Wall. 500. 10.

This article was originally published in a journal ...
Jan 18, 2007 - non-commercial research and educational use including without limitation use in ... Developing a model for adult temperament. David E. Evans.

This article was originally published in a journal ...
An application of data envelopment analysis ... These trends have intensified the need for performance .... Category B: airports in the developing countries.

This article was originally published in a journal ...
and are more active against MAO-A than MAO-B. They also have a relatively low ... During recovery over the next several weeks their density gradually ... had consumed ayahuasca before data collection, at 22, 20, 18,. 16 and 6h (data not ...

This article was originally published in a journal ...
or posting on open internet sites, your personal or institution's website or ... In contrast to the previous studies, we find that on scale-free networks, the density of the recovered individuals ... email service systems, such as the Gmail system sc

This article was originally published in a journal ...
or posting on open internet sites, your personal or institution's .... is, the number of cycles over any given input x, is bounded by jxja a, where a is a ...... [34] H. Rogers Jr., Theory of Recursive Functions and of Effective Computability, rep

This article was originally published in a journal ...
a Institute of Life Sciences, National Taitung University, Taitung 950, Taiwan ... Available online 17 January 2007. Abstract ..... on a computer using Chemstation HP. ..... response to accelerated catabolism of ATP, as occurs in ethanol.

This article was originally published in a journal ...
rent results, the indoor insects should originate from peridomiciliar .... Monteon-Padilla, V.M., Vargas-Alarcon, G., Vallejo-Allende, M.,. Reyes, P.A., 2002. Specific ...

This article was originally published in a journal ...
non-commercial research and educational use including without ... films is important for successful integration in the semiconductor manufacturing technology.

This article was originally published in a journal ...
Jun 30, 2006 - In pulsatile flow conditions, fluid and wall displacements have been measured by Doppler ultrasound methods and quantitatively compared to simulated M-mode ... Keywords: Ultrasound; 3-D arterial model; Arterial mechanics; Wall displace

This article was originally published in a journal ...
Feb 6, 2007 - administrator. All other uses .... NuL, the Nusselt number, based on a characteristic system length. L. Empirical .... Symbol and description. Units.

This article was originally published in a journal ...
Larval body plan diversification is particularly impressive in the visual .... 1d), and serve as templates for a ...... 81–84. Angelini, D.R., Kaufman, T.C., 2004.

This article was originally published in a journal ...
Jul 10, 2006 - or posting on open internet sites, your personal or institution's ... the services sector experience higher increases in per capita footprints, while .... With the development and availability of these data, sociologists have conducted

This article was originally published in a journal ...
or posting on open internet sites, your personal or institution's website or ... comparison of the gene networks controlling larval eye, ocellus, and compound eye ...

This article was originally published in a journal ...
and Atmospheric Administration (NOAA)'s Rapid Update Cycle (RUC20) model as covariates. ..... AOT against observations from the Aerosol Robotic Network.

Hello, Greetings!!! About Company: Enzigma was ... -
Enzigma was exclusively working for Human Concepts LLC till 2007 custom applications, integration and e-commerce. In 2007 Enzigma started working on various projects on. Salesforce.com upcoming platform force.com, the very first cloud platform (PaaS

This article was originally published in a journal ...
non-commercial research and educational use including without limitation ... and Material Science, Zhejiang University of Technology, Hangzhou 310014, China.

(A slightly edited version of this chapter was ...
Reputation-Based Governance and Making States “Legible” to Their Citizens. 13. Reputation-Based Governance and Making ... to effective citizen engagement in governance is making government behavior transparent and understandable, or ... spaces, w

This article was originally published in a journal ...
Available online 17 January 2007. Abstract. Warming from hibernation to ..... The perfusate was advanced into the dial- ysis probe by perfusion of 2 μL at 1 ...