Cursed C - snippets from Expert C Programming

Helpful or not, I’m taking some notes while reading the book “Expert C Programming” by Peter van der Linden.


This post is divided into 3 sections

  • Tips that could be picked up: good habbits, useful snippets, tips. Takeways that are generally good.
  • The black magic: something new, something hacky, I have no idea if I should use them but at least these are cool….
  • Cursed but this is the way: some “twisted” side of the language. But you should learn and accept, because they are well-defined.
  • Pitfalls / Mistakes: undefined behaviours, bad habbits, common mistakes. Or C’s own problems.

Tips that could be picked up.


useful tools (table 6-1 6-2 6-3 6-4)

Tool Where to find what it does
cflow AUR prints the caller/callee relationships of a program
cscope extra interatice c program browser
ldd core/glibc prints dynamic libraries this file needs
nm core/binutils prints symbol table of an object file
strings core/binutils looks at embedded strings


Literal before variable in comparision make debugging easier when a equal sign is missed

1
2
3
if (3==i) 
// instead of
if (i==3)


when to (and not to) use unsigned types
Avoid unnecessary complexity by minimizing your use of unsigned types. Don’t use an unsigned type to represent a quantity just because it will never be negative. Use a signed type like int and you don’t have to worry about boundary cases in the detailed rules for promoting mixed types. Only use unsigned types for bitfields or binary masks. Use casts in expressions to make all the operands signed or unsigned.

(see Pitfalls/implicit_int_conversion)


when to (and not to) use typedefs

Don’t bother with typedefs for structs only to save writing the word struct. And you shouldn’t hide the clue.

Use typedefs for
– types that combine arrays, structs, pointers or functions.
– portable types’
– casts (to have a simpler name to cast to a complicated type)


length array with variant base type
The former allows the base type of the array to change.

1
2
3
#define TOTAL_ELEMENTS (sizeof(array)) / sizeof(array[0]))
// instead of 
#define TOTAL_ELEMENTS (sizeof(array)) / sizeof(int))


Minimal visibility of function
Declaring a function as static storage class makes it only visible within the file. Do this where applicable. Especially for libraries to declare internal only functions…


Let caller allocate the buffer, not the callee

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

// with this, it's easy to forget to free the memory.
char *func(){
    char *s = (char*)malloc(120);
    // do something
    return s;
}

void caller1(){
    char* buffer = func();
    // ?? what is buffer
    // ?? should I free buffer
    // ?? can I safely free it (avoid double free)
}

// better to let caller manage memory:
void func2(char* result, int size){
    // to something
}

void caller2(){
    char* buffer = (char*)malloc(120);
    func(buffer, 120);
    free(buffer)g
}


Use UNION to

  • save space because only one member could exist at once.
  • have different interpretations of the same data.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
union bits32_t {
    int whole;
    struct {char c0,c1,c2,c3;} byte;
};

int main(){
    union bits32_t test;
    int i  = test.whole;
    char c = test.byte.c2;
}


STRUCT, UNION and ENUM takes the same form That’s a trivia, but I’ve never noticed it…

struct  [optional_tag] {stff...} [optional_variable_definitions];
union   [optional_tag] {stff...} [optional_variable_definitions];
enum    [optional_tag] {stff...} [optional_variable_definitions];


reset pointer after free()

1
2
// ensures core-dump for use-after-free mistakes..
free(p); p = NULL;

The black magic


setjmp and longjmp

man setjmp
nonlocal gotos: transferring execution from one function to a predetermined location in another function.

1
2
3
4
5
6
7
#include <setjmp.h>

int setjmp(jmp_buf env);
int sigsetjmp(sigjmp_buf env, int savesigs);

[[noreturn]] void longjmp(jmp_buf env, int val);
[[noreturn]] void siglongjmp(sigjmp_buf env, int val);
  • setjmp() function saves various information about the calling environment (tycally, the stack pointer, the instruction pointer etc.) in the buffer env for later use by longjmp(). setjmp() must be called first
  • longjmp() uses the information saved in env to transfer control back to the point where setjmp() was called and to restore (“rewind”) the stack to its state at time of the setjmp() call. In addition
  • Following a successful longjmp(), execution continues as if setjmp() had return for a second time. This “fake” return can be distinguished from a true setjmp() call because the “fake” return returns the value provided in val. If programmer mistakenly passes the value 0 in val the “fake” return will intead return 1.
  • sigsetjmp() and siglongjmp() also perform nonlocal gotos, but provide predictable handling of the process signal mask.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <setjmp.h>

jmp_buf buf;

void foo(){
    printf("in foo()\n");
    longjmp(buf, 1);
    // non-reachable
    printf("non-reachable\n");
    return; // won't return here
}

int main(){
    if (setjmp(buf)){
        printf("back in main\n");
    }else{
        printf("first time through\n");
        foo();
    }

    return 0;
}

the result will be:

first time thtough
in foo()
back in main

A setjmp / longjmp is most useful for error recorvery. If yo udiscover a unrecoverable error, you can transfer control back to the main input loop and start again from there…

setjmp and longjmp have mutated into the more general exception routines “catch” and “throw” in C++

Cursed but this is the way.


Declaration resembles the use it’s twisted but intended.

1
2
3
4
5
// "an array of pointers-to-integers"
int *p[3];
// to get an integer
i = *p[3]
// hence "declaration resembles the use"

while int *p[3] (as above) means an array of 3 pointers-to-integers, int (*p) [3] means an pointer to an array of 3 integers.

Definition Meaning Usage equi.
int *p[3] array of 3 pointers-to-integers *p[3] int**p
int (*p)[3] pointer of an array of 3 integers (*p)[3] int (*)[3]
int (*fun())() returns pointer to function that return int int i = (*func())()
int (*foo())[] returns pointer to an array of integers int i = (*foo())[3]
int (*foo[])() Foo is an array of int function pointers int i = (*foo[1])()

Fuck me:

1
2
3
4
char *(*c[10])(int **p);

// c is  an array of 10 pointers-to-functions that takes p as parameter and
// returns a pointer to a char. The parameter p is a pointer to a pointer-to int.


Combine typedef with declaration: basically the same semantic.

1
2
3
4
typedef void (*ptr_to_func) (int)

// defines a new type ptr_to_func, which is a pointer to a function that 
// takes an int argument and returns void.

Fuck me 2:

1
2
3
4
5
6
7
8
9
#include <signal.h>
void (*signal(int sig, void (*func)(int)))(int);
// signal is a function that returns a pointer to a function that takes int as
// argument and returns void
// the arguments of signal are an int, and a function that takes int as argument
// and returns void

// With the typedef above:
ptr_to_func signal(int, ptr_to_func);


typedef v.s. #define
Typedef is a complete “encapsulated” type, you can’t add to it after you have declared it.

You can extend a macro typename with other type specifiers but not a typedef’d typename

1
2
3
4
#define my_int int
unsigned my_int i;   // legal
typedef int my_int_t;
unsigned my_int_t i; // illegal

Typedef’d name provides the type for every declarator in a declaration.

See Pitfalls_and_mistakes/how_not_to_declare_multiple_pointers


Qualified types and pointer assignments

in C there are 4 type qualifiers 1

const    (C89) : a value will not be changed. Results of attampt to change is 
                 implementation-defined
volatile (C89) : objects are omitted from optimization because the value can be
                 changed from outside of the current scope at any time.
restrict (C99) : (when used with a pointer), it tells the compiler that ptr is
                 the only way to access the object pointed by it. Vialation is
                 UD - this is C only.
_Atomic  (C11) : to avoid race condition..

in ANSI C: both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed by the right.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
char * cp;
const char *ccp;

// this is legal
ccp = cp;

// this is not legal because *cp has incomplete 
// qualifier compared to *ccp
// [-Wdiscarded-qualifiers]
cp  = ccp;      

// however the following is a bit tricky
char** cpp;
const char** ccpp;
// both these are illegal (at least warning)
// because they are pointers to DIFFERENT TYPES
// [-Wincompatible-pointer-types]
cpp = ccpp;
ccpp = ccp;


pointer is NOT array

It’s said “pointer is identical to array” because array is implicitly converted to pointer as a r-value (or function parameter). The conversion is done by taking the address of the first element.

1
2
3
4
5
int arr[10];
int *ptr = arr;  // arr implicitly converted to int *

void func(int *ptr){}
func(arr)        // arr implicitly converted to int *

The example is from here

But pointer and array are DIFFERENT THINGS:

int * ptr    // ptr is a variable that holds the address of an int
int arr[10]  // arr is a sequence of 10 ints in memory

with extern it goes funky: (assuming both long and pointer are 64bits)

1
2
3
4
5
6
// in file 1.
long arr[] = {1,2,3,4,5};

// in file 2.
extern long * arr;
i = arr[2];         // seg fault

The problem is: arr is declared to be a pointer. Therefore arr[2] means *(arr+2), however arr in file1 is defined to be an array, the value of arr is 1 (cut off the first 64 bits of the array sequence).

Which means, arr[2] finally becomes *(1+2), that’s dereference is garbage.


Precedence… rules?
[TODO]

▸▸ Associativity, what is a=b=c?
All assignment-operator have right associativity, the right most operation in the expression is evaluated first, and evaluation proceeds from right to left.

1
2
3
int a, b=1, c=2;
a = b = c;
// a and b are assigned 2


Multiple function calls in an expression, which first?

1
x = f() + g() * h();

while the evaluation groups g() and h() for multiplication then f() for addition, you can’t assume which function is called first.. So if these functions have side effect that influences each other, don’t mix them in an expression!


Maximal munch

1
2
3
4
5
z = y+++x;
// is parsed to z = y++ + x

// what about z = y+++++x?
// screw you if you write it in the code...


You can return a pointer to string literal

1
2
3
4
5
6
7
8
9
// as we all know this won't work
char* func(){
    char buff[120];
    // do something
    return buff;
}

// but this will (you can't modify the string tho...if it's in read-only memory)
char* func(){return "hello world";}


ANSI and K&R Function Prototypes and Declarations, DO NOT MIX

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// classic c
int bar(void)

// ANSI Prototype
int bar();  // no argument means void
int foo(int a, int b);
int foo(int, int);
// ANSI Definition
int foo(int a, int b){ ... }

// K&R Declaration
int foo();  // no arguments here
// K&R Definition
int foo(a,b)
    int a;
    int b;
{
    ...
}

Either style is supported, but do not mix the usage: if declared with K&R then define in K&R, vice versa!

  • Under K&R, if youp assed anything shorter than an int to a function it actually got an int; and floats were expanded to doubles. The values are automatically trimmed back to the corresponding narrower types in the body of the called function.
  • Under ANSI the parameters are passed “as is” specified in the prototype – the default argument promotions do not occur.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// with K&R
int foo();
int foo(c)
    char c;
{
    // .. expects promoted int.
}


// when foo() is called:
int main(){
    char a = 'a';
    foo(a);

    // what actually happened
    // foo(int(a))      -> promoted to int when passing
    //                  -> in foo() trim back to (char) a
    return 0;
}

Pitfalls / Mistakes


const doesn’t make constant
constqualifier makes the value read-only through that symbol; it doesn’t not prevent the value from being modfied through other means. const is mostly used for qualifying pointer parameter, to indicate that this funtion will not change the data that argument points to.

The combination of const and * is usually only used to simulate call-by-value for array parameters. It says, “I am giving you a pointer to this thing, but you may not change it.


implicit int type conversion

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int main(){
    int i = -1;
    unsigned int j = 20;
    if (i<=j){
        printf("%d is not bigger than %d\n",i,j);
    }else{
        printf("%d is bigger than %d\n",i,j);
    }

    return 0;
}

the result reads: “-1 is bigger than 20”. Because while comparing a unsigned int to signed int, the LHS value is promoted to unsigned and (unsigned int)-1 == 0xffffffff is a LARGE one!


sizeof doesn’t need () but don’t abuse it…

1
int s = 10*sizeof*p;

like, what the hack should this be? And this?

1
apple = sizeof (int) * p;

Trivia: it depends on the type of p. If a pointer, the later is error, if a number, the first is an error.


typedef is pretty free of form but don’t abuse

1
2
3
4
5
6
7
8
// you can use one typedef keyword for multiple declarators but don't.
typedef int *ptr, (*fun)(), arr[5];
// ptr is "pointer to int"
// fun is "pointer to a function returning int"
// arr is an array of 5 ints.

// you can put typedef in the middle of a declaration but DON'T
unsigned const long typedef int volatile *asdlkj;


how not to declare multiple pointers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// p1 is pointer to int, p2 is int. (remember, declaration resembles use, the
// * associates to the right. 
int * p1,p2;

// Or through MACRO
// this is the same as above
#define int_ptr int *
int_ptr p1, p2;

// typedef is possible:
// because we effectively have a new type (though equivalent to int *)
typedef int *int_ptr
int_ptr p1, p2;

Terms from ANSI C

  • Implementation-defined : compiler-writer chooses what happens and how to document it.
  • unspecified : for something correct, on which the standard does not impose any requirements.
  • undefined : for something incorrect, on which the standard does not impose any requirements. Anything is allowed to happen.
  • a constraint : a restriction or requirement that must be obeyed.

[COPYRIGHT & DISCLAIMER]

This article is licensed under CC BY-NC-SA 4.0

All contents in this post, unless stated otherwise, are directly or indirectly taken from the book “Expert C Programming”2. ALL copyrights reserved by the author.


  1. https://en.wikipedia.org/wiki/Type_qualifier and https://www.geeksforgeeks.org/ ↩︎

  2. Expert C Programming - Deep C Secrets, Peter van der Linden. The book is publicly available under https://progforperf.github.io/references.html ↩︎

edited 16.10.2023
created 04.10.2023
EOF
[+] click to leave a comment [+]
the comment system on this blog works via email. The button
below will generate a mailto: link based on this page's url 
and invoke your email client - please edit the comment there!

[optional] even better, encrypt the email with my public key

- don't modify the subject field
- specify a nickname, otherwise your comment will be shown as   
  anonymous
- your email address will not be disclosed
- you agree that the comment is to be made public.
- to take down a comment, send the request via email.

>> SEND COMMENT <<