site-josuah

/usr/josuah
Log | Files | Refs

commit b8d4a3107eaa1f08e6e7efc195577a1d901a19ff
parent 24085761e308974dd98b4239afde787340cc6393
Author: Josuah Demangeon <me@josuah.net>
Date:   Sat,  7 Nov 2020 11:53:31 +0100

adjustments

Diffstat:
Mc-programming/index.md | 31+++++++++++--------------------
1 file changed, 11 insertions(+), 20 deletions(-)

diff --git a/c-programming/index.md b/c-programming/index.md @@ -2,6 +2,7 @@ Bits of C programming ===================== Coding style that helps to isolate unrelated parts of your programs, strive to give a sane and simple API. +Note that I am never happy with what this document says. Naming convention and project hierarchy --------------------------------------- @@ -190,14 +191,15 @@ 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). +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 +### struct -What if more parameter to give? Change all the functions? +In this extreme case: what if more parameter are to give? +Change all the functions? ``` do_something(buf1, len1, prop1, buf2, len2, prop2, buf3, len3, prop3); @@ -209,7 +211,7 @@ Struct gives an outline of the data structures that the program uses: > Choose the right data structures and the program will write itself -### enum is the sight +### enum In switch statements and in code: @@ -341,18 +343,6 @@ 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. - Reentrant function without specifying the buffer ------------------------------------------------ Reentrancy is a property of a function which ensure that even if it is being @@ -368,7 +358,8 @@ fill. For example localtime() is returning a pointer to a (struct tm *), but no such structure is passed to the function: a same global or static variable is used for all calls to localtime, which makes it vulnerable -while using threads. localtime_r() is to be used instead. +while using threads. +localtime_r() is to be used instead. It is possible to pass a chunk of the stack memory of the calling function without having to declare a variable through compound literal and a macro: