Tag: C

Exiting on malloc(3) Failure

To the annoyance of many students learning C programming, allocating memory by calling malloc(3) can fail. When it does, malloc(3) will return NULL. But, what should our program do when a call to malloc(3) fails? The question that today’s blog post explores is: when is it okay for our program to exit(3) — that is, simply terminate — because of a memory-allocation failure?

Better printf(3) Debugging in C

In today’s blog post, we’ll look at one way to improve printf(3) and fprintf(3) debugging in C. Namely, let’s make it so our printf(3) debugging outputs include the name of the source code file, the function name, and the line number where the output happens. Debugging with printf(3) is fast and easy, and it’s not going anywhere; so, let’s just make its output more useful.

Constant Pointers in C: Reading Right to Left

The “const” keyword in the C programming language, when applied to pointers, can be quite confusing to read. How do you read a variable declaration like “const char *const *const p”? This blog post explains how to read these declarations, and demonstrates the technique of reading them from right to left.

Aborting on Programming Errors

Programming errors can be as small and subtle as an off-by-one error in a loop, or as large as a flaw in the implementation or design of an important algorithm. What should our code do when it runs into this type of error? In most cases, we shouldn’t be afraid just to make our program abort.