Log | Files | Refs

commit b9a4870df295e1fbc69d5a6a0a9b0253241c7dbf
parent 3130cb95171b22372ada867ccb22b94d45a2a54b
Author: Josuah Demangeon <>
Date:   Sat,  1 Aug 2020 17:07:36 +0200

add a reference to netini

Diffstat: | 1+
Mwiki/c-coding-style/ | 41+++++++++++++++++++++++++++++++++++++++--
2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/ b/ @@ -7,6 +7,7 @@ You can find documentation about my software projects: * The [sni-shunt](// tool, dispatch TLS session according to server-name. * The [passlock](// tool, an authentication backend. +* The [netini](// toolbox, to plot networks. * The [ics2txt](// tool, to deal with the iCalendar format. * My [githooks](/wiki/git-hooks/) scripts, that regenerate my NotWiki sites. diff --git a/wiki/c-coding-style/ b/wiki/c-coding-style/ @@ -7,7 +7,6 @@ Naming convention and project hierarchy * ./Makefile (and,, Makefile.linux, Makefile.bsd...) * ./README, ./ * ./progname.c -* ./progname.1 * ./doc/rfc3564.txt, ./doc/progname.1 * ./src/base64.c, ./src/random.c, ./src/conf.c @@ -28,6 +27,13 @@ assert(), assert(), assert() Error handling -------------- +While other programming languages planned special keywords like try/catch and +features like exceptions, automatic cleanup at function exit (useful while +needing to return early, like when an error occurs), C lets the user handle +error with `if (failure_condition) { handle_failure; }`. + +Some idioms permit error handling to be as inobstrusive in C as in any other +languages: ### Error handling if() style @@ -183,6 +189,10 @@ Known as "the only right use of goto". (Ab)use of types ---------------- +While abused, types makes the code more opaque and harder to read. But used +wisely, they are the key for handling structured data and build useful +abstractions, which often come for no extra cost in compiled languages that +work without a runtime type system (but a compile-time one). ### struct is the key @@ -196,7 +206,7 @@ What if more parameter to give? Change all the functions? Struct gives an outline of the data structures that the program uses: - "Choose the data structures and the program will write itself" +> Choose the right data structures and the program will write itself ### enum is the sight @@ -244,6 +254,21 @@ Initialize fields: Memory management ----------------- +C memory management plays around pointers: a variable holding a position in +memory that is available for the program to use. + +For growing a region of available memory (a buffer), it is sometimes needed +to move memory somewhere else, which changes the pointer: the address in memory +locating the buffer. + +Pointers are useful for multiple data structures, for referring another element +such as the `->next` element of a linked list. + +If a buffer is refered from by one of these data structures, but the buffer +grows, the reference will therefore be invalid. + +Combining memory management and pointers gets done through checking when a +buffer is immutable (and will not change anymore), or may still be modified: ### Same struct type, various size: @@ -314,3 +339,15 @@ memory grows. Variable-size struct member as shown before: only when size does not change in advance. + +Safe wrapper for string and memory management +--------------------------------------------- +C is a very unsafe programming language when used badly. It is still unsafe while +used nicely, but slightly less. It is even less unsafe with simple wrapper over +the buffer and memory management primitives. + +Calculating the length and then filling the memory is dangerous: length +miscalculation happen often, and this is a very frequent operation (for growing +strins and buffers). + +These two operation needs to be coupled.