Posts Tagged C

 

Reviving the 1973 Unix text to voice translator

The early Research Edition Unix versions featured a program that would turn a stream of ASCII text into utterances that could be played by a voice synthesizer. The source code of this program was lost for years. Here’s the story of how I brought it back to life.

Continue reading "Reviving the 1973 Unix text to voice translator"

The Origins of Malloc

The 1973 Fourth Edition Unix kernel source code contains two routines, malloc and mfree, that manage the dynamic allocation and release of main memory blocks for in-memory processes and of continuous disk swap area blocks for swapped-out processes. Their implementation and history can teach us many things regarding modern computing.

Continue reading "The Origins of Malloc"

Of BOOL and stdbool

The C99 standard has added to the C programming language a Boolean type, _Bool and the bool alias for it. How well does this type interoperate with the Windows SDK BOOL type? The answer is, not at all well, and here’s the complete story.

Continue reading "Of BOOL and stdbool"

Programming Languages vs. Fat Fingers

A substitution of a comma with a period in project Mercury's working Fortran code compromised the accuracy of the results, rendering them unsuitable for longer orbital missions. How probable are such events and how does a programming language's design affect their likelihood and severity? In a paper I recently presented at the 4th Annual International Workshop on Evaluation and Usability of Programming Languages and Tools I showed results obtained by randomly perturbing similar programs written in diverse languages to see whether the compiler or run-time system would detect those changes as errors, or whether these would end-up generating incorrect output.

Continue reading "Programming Languages vs. Fat Fingers"

How to Calculate an Operation's Memory Consumption

How can you determine how much memory is consumed by a specific operation of a Unix program? Valgrind's Massif subsystem could help you in this regard, but it can be difficult to isolate a specific operation from Massif's output. Here is another, simpler way.

Continue reading "How to Calculate an Operation's Memory Consumption"

Applied Code Reading: Debugging FreeBSD Regex

When the code we're trying to read is inscrutable, inserting print statements and running various test cases can be two invaluable tools. Earlier today I fixed a tricky problem in the FreeBSD regular expression library. The code, originally written by Henry Spencer in the early 1990s, is by far the most complex I've ever encountered. It implements sophisticated algorithms with minimal commenting. Also, to avoid code repetition and increase efficiency, the 1200 line long main part of the regular expression execution engine is included in the compiled C code three times after modifying various macros to adjust the code's behavior: the first time the code targets small expressions and operates with bit masks on long integers, the second time the code handles larger expressions by storing its data in arrays, and the third time the code is also adjusted to handle multibyte characters. Here is how I used test data and print statements to locate and fix the problem.

Continue reading "Applied Code Reading: Debugging FreeBSD Regex"

HP-200LX Remote Control Hacks

All my friends know that for the past 15 years I've been semi-attached to an HP 100LX palmtop PC (recently updated to a 200LX) for my personal information management and many other tasks. The device is extremely versatile, sturdy, and flexible. Amazingly, after so many years of hard daily use, I still find new applications for it.

Continue reading "HP-200LX Remote Control Hacks"

Real-Time Google Earth GPS Tracking

In a recent trip I incorrectly assumed that real-time tracking of Google Earth's pre-cached maps with a GPS receiver would be sufficient help for navigating around the highways in Los Angeles. I therefore experimented with the way Google Earth's sparsely-documented real time tracking works, and wrote a small program to interface Google Earth with a GPS receiver. Fortunately, after seeing a colleague drive with a car-GPS device on the dashboard I came to my senses, and got a real Garmin Nuvi car-GPS device.

Continue reading "Real-Time Google Earth GPS Tracking"

The Escape of a Small Program

C. A. R. Hoare's Law of Large Programs states that inside every large program is a small program struggling to get out. The parking receipt I got yesterday returning from a SQO-OSS meeting proves this fact.

Continue reading "The Escape of a Small Program"

Code Finessing

When I set out to apply CScout on the Linux kernel source code, I discovered that it failed to correctly expand a couple of C macros, causing the analysis to fail. This prompted me to reimplement CScout's macro expansion using a precise functional specification, then optimize the code's severe degradation in time performance, and finally tidy up the optimized code mess.

Continue reading "Code Finessing"

The Verbosity of Object-Oriented Code

As I refactored a piece of code from an imperative to an object-oriented style I increased its clarity and reusability, but I also trippled its size. This worries me.

Continue reading "The Verbosity of Object-Oriented Code"

Dave Prosser's C Preprocessing Algorithm

For about five years I've been trying to implement a fully conforming C preprocessor for the front end of the CScout refactoring browser. I've found this to be a fiendishly difficult task. Although what I have written can correctly process million-line real-life projects, every once in a while I come across a construct that confuses my implementation. While searching the web for explanations of some of the finer points of the C standard I came across a reference to an algorithm by Dave Prosser that the X3J11 (ANSI C standard) committee used as a basis for the standard's wording.

Continue reading "Dave Prosser's C Preprocessing Algorithm"

Interoperability Requires Temperance

After testing the CScout refactoring browser on the FreeBSD kernel, I decided to try it on Linux. I'm getting there, but slowly, and the reason is the gratuitous use of gcc extensions made in the Linux kernel source code. Every time I come across a program construct that CScout doesn't grok, I have to study the C standards to see if the construct is legal C that CScout fails to implement or a gcc extension. Extensions are trouble, because, they're typically only vaguely documented.

Continue reading "Interoperability Requires Temperance"

Code Quality: The Open Source Perspective

My new book Code Quality: The Open Source Perspective got published, three years after I started writing it. The book owes more to open source software than any of the books dealing with Linux, PHP, Apache, Perl or any other book covering a specific technology.

Continue reading "Code Quality: The Open Source Perspective"

A General-Purpose Swap Macro

A couple of days ago I came up with a general-purpose macro for swapping values in C programs. My colleague Panagiotis Louridas suggested an improvement, and this prompted me to see the two macros got compiled.

Continue reading "A General-Purpose Swap Macro"

How to Sort Three Numbers

Quick: how do you sort three numbers in ascending order?

Continue reading "How to Sort Three Numbers"

Warum einfach, wenns auch kompliziert geht?

(Why make it simple, when you can also make it complicated?) Consider the task of associating code with specific data values. Using a multi-way conditional can be error-prone, because the data values become separated by the code. It can also be inefficient in the cases where we have to use cascading else if statements, instead of a switch, which the compiler can optimize into a hash table. In C I would use an array containing values and function pointers. My understanding is that the Java approach involves using the Strategy pattern: a separate class for each case, and an interface "to rule them all".

Continue reading "Warum einfach, wenns auch kompliziert geht?"

Macro-based Substitutions in Source Code

A friends asks: "How can one easily replace a method call (which can contain arguments with brackets in its invocation code) with a simple field access?

Continue reading "Macro-based Substitutions in Source Code"

The hypot() Mystery

I was writing a section for the Code Reading followup volume, and wanted to demonstrate the pitfalls of using homebrewn mathematical functions instead of the library ones. As an example, I chose to compare the C library hypot(x, y) function, against sqrt(x * x, y * y). I created a plot of "unit in last place" (ulp) error values between the two functions, which demonstrated how the error increased for larger values of y.

Continue reading "The hypot() Mystery"

Patching Framework III

Time warp. I needed to read some old files I wrote in 1992 using the Ashton-Tate Framework III program. Unfortunately, trying to run the program under Windows XP resulted in a "Divide overflow" error. A bit of searching on the web revealed that the problem was related to the system's speed (1.6GHz). Apparently, Framework tries to calculate the speed of the machine by dividing a fixed number with a loop counter; on modern machines this results in the overflow.

Continue reading "Patching Framework III"

Computer Languages Form an Ecosystem

(This is a copy of an article I posted on slashdot on March 15th, in response to a discussion titled C Alive and Well Thanks to Portable.NET. Many posters argued that the C language is dead. I add my response here, because one month after its original slashdot submission, I am still getting web site hits from it.)

Continue reading "Computer Languages Form an Ecosystem"

Become a Unix command line wizard
edX MOOC on Unix Tools: Data, Software, and Production Engineering
Debug like a master
Book cover of Effective Debugging
Compute with style
Book cover of The Elements of Computing Style
Syndication
This blog is also available as an RSS feed:

Category Tags
AI (4)
AWS (4)
Android (2)
Apple (11)
C (21)
C++ (17)
Computers (58)
Databases (6)
Debugging (10)
Discussion (6)
Electronics (15)
Environment (1)
FreeBSD (26)
Funny (14)
GSIS (5)
Git (2)
Google (6)
Government (3)
Hacks (26)
Hardware (27)
History (13)
Information systems (1)
Internet (12)
Java (26)
JavaScript (1)
Linux (7)
Management (27)
Microsoft (11)
One Laptop Per Child (3)
Open source (58)
Opinion (30)
Parenting (11)
Perl (13)
Photos (13)
Politics (5)
Programming (110)
Python (3)
R (1)
Raspberry Pi (6)
Risks (7)
Scala (1)
Science (35)
Security (26)
Sights (19)
Smartphones (3)
Software (22)
Software engineering (93)
Standards (7)
System administration (46)
Teaching (9)
Technology (33)
Testing (3)
Tips (43)
Tools of the Trade (52)
Travel (9)
UML (6)
Unix (53)
Web (31)
Windows (17)
Writing (46)
XML (10)
vim (5)
Archive
Complete contents (382)
2024 (3)
2023 (5)
2022 (2)
2021 (3)
2020 (15)
2019 (4)
2018 (5)
2017 (20)
2016 (7)
2015 (6)
2014 (5)
2013 (13)
2012 (17)
2011 (14)
2010 (13)
2009 (40)
2008 (40)
2007 (41)
2006 (48)
2005 (44)
2004 (30)
2003 (7)

Last update: Tuesday, October 15, 2024 2:19 pm

Creative Commons Licence BY NC

Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.