Compiler demons of uninitialization.

Started by
1 comment, last by outRider 17 years, 12 months ago
Innocently enough here we have a nice little table of colours that one would expect to always exist and never get changed:

struct ColourInfo
 {
  const char *name;
  const Colour colour;
 };

static const ColourInfo colours[] =
 {
  {"Alizarin Crimson",     Colour(227, 38,  54)},
  {"Amber",                Colour(255, 191, 0)},
  {"Amethyst",             Colour(153, 102, 204)},
  {"Aqua",                 Colour(0,   255, 255)},

// snip. . .


However, things aren't quite as simply as they appear. Meanwhile, down farther in the very same file:

static Colour::Name stringToName(const std::string &name)
 {
  for(unsigned int c = 0; c < sizeof colours/sizeof *colours; ++c)
   {
    const char *in1(colours[c].name);
    
    assert(in1);

// snip. . .


The universe explodes. Luckily my savior GDB came to the rescue, and told me the following:

(gdb) p c
$1 = 0
(gdb) p colours[c]
$2 = {name = 0x0, colour = {b = 0 '\0', g = 0 '\0', r = 0 '\0', a = 0 '\0'}}
Sadly, it seems some dark mage cast 'blight of non-existance' on my colour table. And how did he do it? Did he use a buffer overflow to clobber my table? No, he did something even more ghastly. He created a global variable that accessed the table through its constructor before it was even created. How fiendish is that? I guess the moral of the story is that no matter how innocent, or constant a global variable looks, it's pure evil and should not be trusted. No, I didn't want any help. I just felt like complaining.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
Quote:Original post by smart_idiot
I guess the moral of the story is that no matter how innocent, or constant a global variable looks, it's pure evil and should not be trusted.
Been there - I had a similar construct that magically stopped working when I switched compilers. Of course it took me hours to figure out what the problem was, so I learned my lesson.
Maybe you have a messed up linker script and/or crt0? Are you using GCC for a non-mainstream platform?

This topic is closed to new replies.

Advertisement