‘c’ Archive

Caution required with integer conversions Wednesday 2nd January 2008 Comments Off on Caution required with integer conversions

This applies to both C and C++, although the details are a little different in the more obscure corners. Consider this C++ snippet which is designed to position a streambuf somewhere a bit before its end: pos = sb->pubseekoff( -128 – thing.GetSize(), ios_base::end, ios_base::in ); How did this fail spectacularly? OK, here are the types. […]

extern arrays and pointers Saturday 17th November 2007 Comments Off on extern arrays and pointers

Quick, what’s the difference! extern char data[]; extern char *data; Well the first one’s and array and the second one is a pointer. You can treat them in the same way, but they are completely different things. Here’s a function which looks the same with both declarations of data: char f(int a) { return data[a]; […]

restrict, part II = x86_64 Thursday 26th July 2007 Comments Off on restrict, part II = x86_64

Just to make some use of my cross compiler, here’s how the assembler for x86_64 stacks up for the restrict example which I posted before. Without restrict: fibincr2: movl (%rdi), %eax # eax = *a A addl (%rsi), %eax # eax += *b A + B movl %eax, (%rdi) # *a = eax A + […]

Cross-compiling gcc, glibc and all that. (Part II – the script) Wednesday 18th July 2007 Comments Off on Cross-compiling gcc, glibc and all that. (Part II – the script)

crosscomp.sh So here it is, the script that does it all. Although it is a shell script, I thorougly recommend not running it, but cutting and pasting it, section by section, into a terminal session so that you can fix up any environmental issues that cause it to fall over. There are some paths at […]

Cross-compiling gcc, glibc and all that. Monday 16th July 2007 Comments Off on Cross-compiling gcc, glibc and all that.

Yippee! I finally managed to get a working cross-compiler x86 -> x86_64 on one of my linux boxes setup with c++ and shared library support. gcc, the kernel and glibc have the most annoying set of interdependencies so doing a ground up build is horribly painful, consisting of several rounds of bootstrapping. Like most people […]

Building code (Part II) – dependency generation Wednesday 27th June 2007 Comments Off on Building code (Part II) – dependency generation

Automatic dependency generation can make a huge difference on productivity. If you have a large project then building every source file, every time in a code and fix cycle can grind the process to a halt. Likewise, if your build process doesn’t rebuild any object file that already exists, or only rebuilds it when the […]

Building Code (Part I) – Makefiles Tuesday 26th June 2007 Comments Off on Building Code (Part I) – Makefiles

Makefiles are one of the things that I don’t like spending a lot of time working with, they’re there to support writing software so they should be easy and simple to work with, but they should “just work”. So naturally, I’ve spent a lot of time working on a setup that “just works”. There are […]

restrict, yes it does make a difference Friday 22nd June 2007 Comments Off on restrict, yes it does make a difference

restrict is a new keyword in C99 (ISO/IEC 9899:1999) designed to enable the compiler to make more optimizations in response to programmers’ guarantees. You can read the full meaning of the keyword in the standard but an oversimplification would be that if a pointer is marked as restrict then it is a guarantee that the […]