Skip to main content

3 posts tagged with "C Programming"

C programming concepts and techniques

View All Tags

Writing Portable C Code: Preprocessor Directives, Data Types, and GNU Autotools

· 45 min read
Pranav Ram Joshi
Software Engineer — Systems & Networks

Preamble

Writing a portable C program turns out to be more of a nightmare than imagined. C is notorious for being platform dependent--also known as portable assembly language, yet most of the software that runs across different systems is written in this language. The secret lies in understanding what varies between platforms--data type widths, byte ordering, memory alignment, available system calls and library functions--and using the right tools to handle those differences at build time. In this post, we'll walk through the C compilation pipeline, explore how preprocessor directives enable conditional compilation, examine data type portability pitfalls like endianness and struct padding, and introduce GNU Autotools as a build system for portable projects. This is the third in a three-part series. The first post covers UNIX terminal devices, and the second explains how the shell executes programs.

Berkeley Packet Filter (BPF): Packet Capture and Filtering in C

· 32 min read
Pranav Ram Joshi
Software Engineer — Systems & Networks

Be conservative in what you do, be liberal in what you accept from others. - RFC 793

Preamble: From Telecommunication to Packet Filtering

Throughout the course of humanity, we have witnessed various forms of communication. We use language to communicate on a daily basis. Writing letters is also a form of communication, albeit a form which has slow transmission and reception time. Then came the era of telecommunication. Through the use of electronic and electrical means, we soon came to the realization that communication can be instantaneous. This, I believe, is one of the greatest achievement for mankind. This really made the world feel like a small village. The Internet is the closest to the anywhere door we know from Doraemon.

Use-After-Free in C: Why It Happens, How Static Analyzers Catch It, and What Rust Does Differently

· 11 min read
Pranav Ram Joshi
Software Engineer — Systems & Networks

Introduction: Why Use-After-Free Doesn't Always Crash

Memory is a crucial topic when it comes to building software. Programs running on historical devices referenced physical memory locations directly, a practice superseded by a dedicated hardware component: the Memory Management Unit (MMU). We now work with virtual memory addresses that is managed by MMU for us[0]. For a hands-on exploration of how process memory is laid out on macOS ARM64, see the memory post.

malloc and free: Why Accessing Freed Memory Can Silently Succeed

I recently stumbled across a video regarding the RustTM language. The intent of this post is not to look down on the RustTM language but to observe a behaviour of a trivial C program. A program fragment was shown in the video, similar to the one shown in Listing 1. After building the executable, we can notice that the program exits normally. But before that, we tried to access a region of memory that was "freed". In contrast, RustTM informs this issue during compilation.