Need to learn C, best books?

Andrew Black andrew-li at black1.org.uk
Wed Oct 24 20:33:58 BST 2007


Andy Armstrong wrote:

> The main pain for a Perl programmer will be understanding the extent of 
> your responsibility for memory management and coming to terms with the 
> potential for shooting yourself in the foot. Just because a program 
> appears to be working doesn't mean that you're not doing things like 
> reading beyond the end of an allocated block. It's quite possible, for 
> example, to trample on a local variable in a way that isn't apparent 
> until you change something later and start getting what seem to be 
> random crashes. No safety net.

I once had a really mysterious crash in a bit of C code that hadn't 
changed in years.  I removed the changes one by one to take me back to a 
working version and found the crash happened only if a particular line 
as added to a totally unrelated bit of code.

After a lot of instruction level debugging, I figured out
  - a function was writing a 0 byte off the end of an array
  - this was corrupting the return address on the stack
  - code was returning to the wrong instruction, but it wasn't so bad as 
to crash (or noticeable give wrong results).
  - my unrelated change moved the above code up a bit, so it was 
returning to somewhere that was "wronger". Presumably the code was 
making wrong assumptions about what was pushed on the stack and 
eventually fell over in a real heap.


More information about the london.pm mailing list