2024-11-27

C (Programming Language)

Table of Contents

1. Pointers, UB and Optimization

2. Against C

  • About C standard trying to not introduce warnings hinder the growth and safety of C: Why the C Language Will Never Stop You from Making Mistakes
    • C languages prioritizes backwards compatability
    • New warnings are considered breaking behaviour, so obvious safety improvements are ignored
    • C committee resists changes that might disrupt old coldbases
  • C must die: https://veresov.pro/cmustdie/ [Russian]
    • Initially, C simplified low-level programming and portability but C is now unpredictable. This is because of its abstract machine model and undefined behaviors combined with aggressive compiler optimization.
    • Compilers exploit undefined behavior for optimization, leading to unexpected results, security vulnerabilities, and non-portable programs.
    • Developers like Linus Torvalds criticize the C standard's detachment from reality. However, compiler developers often prioritize strict adherence to the standard, without regard for practical consequences.

3. Debugging C

3.1. Address Sanitizer

You can compile C/C++ program with Address santizer which checks for memory leaks, use after free, and heap overflow. This helps catch bugs early on and makes debugging easier.

During compilation add -fsanitize=address (optionally add debug symbols -g and preserve stack traces -fno-omit-frame-pointer). And during linking add statically link asan library -static-libasan.

gcc -c main.c -fsanitize=addresss -g
gc main.o -o main -fsanitize=address -static-libasan

Or when compilation and linking is done is single step:

gcc main.c -o main -fsanitize=address -g -static-libasan

Now when those memory related error happen the program will crash with a report that says where the memory was allocated, and where it was misused; along with stacktraces at both locations.

4. Misc

  • Kitsune: Efficient, General-purpose Dynamic Software Updating for C

    Loads your program as a shared library, and allows updating the program while it is running. It works by allowing programmers to specify some points in program where update maybe done. And provides facilities to do data migration.

    It was suppored by ExaScale Computing Project.


Backlinks


You can send your feedback, queries here