Singing Potatoes
Friday, 21 March 2003
Losing my touch

Warning: dull programmer's rant.

I've just spent the better part of two days bashing my head against a program that insisted on crashing when I allocated memory. Not when I wrote to the allocated memory, but the actual act of allocating memory caused the program to crash, despite having 38 physical megabytes of memory free.

I put all sorts of temporary lines in there to spit values into a debugging file, saw no problems. I went through it line by line with the debugger, over and over again, saw no problems except that malloc() caused a GPF. I replaced the native C malloc() calls with Windows-specific GlobalAlloc() calls. It continued to crash. I moved the memory allocation from the DLL segment of my application into the main executable, just in case that was causing problems. It continued to crash.

Finally, I found the problem, in a different section than the one that appeared to be causing the crash:

t = malloc(sizeof(s)+1);

... is not the same as:

t = malloc(strlen(s)+1);

Bah! That's the stupid crappy sort of mistake I used to make when I was first learning C. Now I can barely remember what the hell I was trying to do in the first place, since this problem had consumed me to the point of wanting to throw my computer out the window.

Oui, c'est moi, l'idiot.

Posted by godfrey (link)