This presentation by Andrey Lukin (Senior Software Engineer, Consultant, GlobalLogic, Kyiv) was delivered at GlobalLogic Kharkiv Embedded Conference 2019 on July 7, 2019.
Andrey demonstrated models of working with complex expressions in C that require perfect knowledge of the standard. Throughout the report, he has sorted out the best and worst examples of code.
Conference materials: https://www.globallogic.com/ua/events/kharkiv-embedded-conference-2019/
4. 4
Some obscure moments: ternary operator
x ? : y
from Android’s QEMU:
machine->max_cpus = machine->max_cpus ?: 1; /*
Default to UP */
https://android.googlesource.com/platform/external
/qemu/+/master/vl-android.c#3472
6. 6
Some obscure moments: ternary operator
x ? x : y
It’s ok when 1st operand contain a side effect
but with -pedantic you’ll have the following complaints from
compiler:
warning: ISO C forbids omitting the middle term of a ?:
expression
8. 8
Some obscure moments: ternary operator
status_color = frobs < 10 ? CODE_GREEN
: frobs < 15 ? CODE_YELLOW
: frobs < 20 ? CODE_RED
: CODE_PURPLE;
At least format it like that
9. 9
A bit more of potential mistakes
from some real driver:
if(strlen(ops->name)) {
strncpy(n->name, ops->name,
(strlen(ops->name) > 79)?79:strlen(ops->name)
);
}
10. 10
A bit more of potential mistakes
from some real driver:
if(strlen(ops->name)) {
strncpy(n->name, ops->name,
(strlen(ops->name) > 79)?79:strlen(ops->name)
);
}
--> use strscpy(), for instance
11. 11
Let’s remember your interview for C position
void (* signal(int __sig, void (* __func)(int))) (int)
12. 12
Let’s remember your interview for C position
void (* signal(int __sig, void (* __func)(int))) (int)
Yes, you are right. This is from some old libc…
And can be found even now, for instance, in Microchip
sources
13. 13
Let’s remember your interview for C position
void (* signal(int __sig, void (* __func)(int))) (int)
Uncovering the magic
14. 14
Let’s remember your interview for C position
void (* signal(int __sig, void (* __func)(int))) (int)
Uncovering the magic
15. 15
Let’s remember your interview for C position
void (* signal(int __sig, void (* __func)(int))) (int)
Uncovering the magic
16. 16
Let’s remember your interview for C position
void (* signal(int __sig, void (* __func)(int))) (int)
Uncovering the magic
17. 17
Let’s remember your interview for C position
void (* signal(int __sig, void (* __func)(int))) (int)
Uncovering the magic
18. 18
Let’s remember your interview for C position
/* Type of a signal handler. */
typedef void (*__sighandler_t) (int);
...
extern __sighandler_t __sysv_signal (int __sig,
__sighandler_t __handler)
__THROW;
glibc developers were wise from the very beginning!
26. 26
Emoji
:-!!
/* Force a compilation error if condition is true, but also produce a
result (of value 0 and type size_t), so the expression can be used
e.g. in a structure initializer (or where-ever else comma expressions
aren't permitted). */
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
Maybe better to name it ...OR_ZERO()
28. 28
Best: Use coding standards
A coding standard’s purpose is to restrict use of problematic
areas of the programming language.
29. 29
Best: Use coding standards
A coding standard’s purpose is to restrict use of problematic
areas of the programming language.
So, using coding standards prevents undefined or unspecified
behavior. And it limits the use of error-prone constructs,
such as "goto".
30. 30
Best: Use coding standards
A coding standard’s purpose is to restrict use of problematic
areas of the programming language.
So, using coding standards prevents undefined or unspecified
behavior. And it limits the use of error-prone constructs,
such as "goto".
And using coding standards also improves the code’s
readability, maintainability, and portability. One example is
the use of “typedef” to simplify the syntax of complex
structures.
31. 31
Best: Use coding styles
For instance, Linux kernel coding style
(remember checkpatch.pl)
● indentation
● breaking long lines and strings
● placing braces and spaces
● ...
32. 32
Best: Use coding styles
Or GNU C coding style, for instance:
if (foo)
if (bar)
win ();
else
lose ();
33. 33
Best: Use coding styles
Or GNU C coding style, for instance:
if (foo)
{
if (bar)
win ();
else
lose ();
}
34. 34
Best: Use coding styles
Or GNU C coding style, for instance:
if ((foo = (char *) malloc (sizeof *foo)) == NULL)
fatal ("virtual memory exhausted");
35. 35
Best: Use coding styles
Or GNU C coding style, for instance:
if ((foo = (char *) malloc (sizeof *foo)) == NULL)
fatal ("virtual memory exhausted");
instead, write this:
foo = (char *) malloc (sizeof *foo);
if (foo == NULL)
fatal ("virtual memory exhausted");
36. 36
Best: Use coding styles
Вопрос на форуме:
Уменязапалпробел.Чтомнеделать?
Ответ:
настоящие_программисты_пробелами_не_пользуются
или
НастоящиеПрограммистыПробеламиНеПользуются
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element
TEXT AND IMAGE
– Headline 32pt Arial in BLACK
– Body copy & bulleted text 12pt Arial Reg in GRAY; body copy not to go below 10 pt
– Left-justify all text and design element