• Advertisement
Sign in to follow this  

Compiler demons of uninitialization.

This topic is 4330 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Maybe you have a messed up linker script and/or crt0? Are you using GCC for a non-mainstream platform?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement