Feeds

Write Portable Code

Solid principles for C and C++ developers

  • alert
  • submit to reddit

Maximizing your infrastructure through virtualization

Book review Portability is one of those virtues we would all love to have in our code. One of the drivers of Java's popularity was the promise of portability across platforms, and of course that same promise exists in many of the more popular scripting languages, such as Python, Ruby and so on.

However, when it comes to coding in C or C++, portability is much, much harder to achieve, simply because coding in these languages tends to be closer to the hardware. Without a virtual machine to code to, the vagaries of real platforms makes even the best of developers hostage to the operating system and the metal it runs on. However, help and advice is on hand for C/C++ coders in the form of Write Portable Code, which might easily have been entitled How to write portable code in C or C++'.

The book opens with a discussion of what portability means and how difficult it is to take a single platform product and port it across to another platform. Unless, of course, portability has been part of the design thinking from the start. Portability, like security, is better as part of the fundamental design than as an after-thought.

Having explored the underlying concept, the book moves on to examine in more detail the technical and logistical issues that have to be faced. These range from differences in hardware, such as processors and floating point number formats, to differences in file systems, memory addressing, dynamic libraries and so on. Such differences can be a nightmare to deal with, and the book explores a variety of strategies for dealing with them.

One of the key ingredients in cross-platform code is the ability to inspect the current platform. This means much more than just querying to find out about the operating system the software is running under. Issues like big-endian vs. little-endian architectures can make even basic functions difficult. Author Brian Hook provides a partial solution in the form of POSH (Portable Open Source Header), which provides several platform introspection functions.

It's not just platform differences that can trip up the unwary. The book also looks at the logistics of coding in different platforms, from compiler differences to source control to editors, IDEs and other tools. Again, as with the other parts of the book, the author's knowledge and experience shines through, as does his writing skill.

A big plus for the book is the sample application, SAL (Simple Audio Library), which puts into practice the principles that are discussed throughout the book. Full source code for SAL and POSH are available for download, making it relatively easy to get started with code.

While the emphasis is on C and C++, other languages do get a mention. Scripting languages in particular are covered in a separate chapter that looks at JavaScript, Python and Ruby. In particular the use of an embedded interpreter is explored, showing how it can be used to code around platform differences but also shows what the trade-offs are in terms of performance and other factors.

To conclude, this book provides the reader with a mixture of solid principles, concrete examples and an extended example using a portable sound library that illustrates how the principles translate into the real world.

Write Portable Code

Author: Brian Hook

Publisher: No Starch Press

ISBN: 1593270569

Media: Book

Level: Intermediate/Advanced

Verdict: Highly practical and very useful

Buy this book at Cash & Carrion

Reducing security risks from open source software

More from The Register

next story
HIDDEN packet sniffer spy tech in MILLIONS of iPhones, iPads – expert
Don't panic though – Apple's backdoor is not wide open to all, guru tells us
Do YOU work at Microsoft? Um. Are you SURE about that?
Nokia and marketing types first to get the bullet, says report
Microsoft takes on Chromebook with low-cost Windows laptops
Redmond's chief salesman: We're taking 'hard' decisions
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
EU dons gloves, pokes Google's deals with Android mobe makers
El Reg cops a squint at investigatory letters
Chrome browser has been DRAINING PC batteries for YEARS
Google is only now fixing ancient, energy-sapping bug
prev story

Whitepapers

Seven Steps to Software Security
Seven practical steps you can begin to take today to secure your applications and prevent the damages a successful cyber-attack can cause.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
Designing a Defense for Mobile Applications
Learn about the various considerations for defending mobile applications - from the application architecture itself to the myriad testing technologies.
Build a business case: developing custom apps
Learn how to maximize the value of custom applications by accelerating and simplifying their development.
Consolidation: the foundation for IT and business transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.