To caps or not to caps that is the question?

Steve smorrey at gmail.com
Tue Dec 18 04:14:58 MST 2007


Howdy folks,

I have a bit of C++ I wrote specifically to randomly generate fantasy
style names.
It all seems to work great until I try to run an optimizing pass on it.
Essentially the code works like this.
We have two lists of letters, one called consonants and the other is vowels.
About 2/3rds of the time we want to pick a vowel, the other 1/3rd
should be a consonant.
We want to try to avoid double consonants, but double vowels are ok,
and it's expected a few names are just simply going to end up being
incomprehensible.
Also if it's our first time picking a letter we want to capitalize it.

Now here's the rub, if I do not run -O2 or -O3 when I compile it, the
code works exactly as ordered.
However if I do run it through the optimizer the program doesn't
bother to capitalize!

Here are the relevant lines.
     for(; counter < length; counter++)
     {
          coin = RandomNumber(0,100);
          letter = (letter == oldletter) ? ((coin <= 60) ?
PickChar(vowels) : PickChar(consonants)) : letter;
          letter = (counter == 0) ? toupper(letter) : letter;
          name += letter;
          oldletter = letter;
     }
     return(name);

What I can't for the life of me figure out is why the optmizing phase
of the compiler would skip the line where it says to capitalize.
I need to see if there is a way to prevent this in the future since
simply skipping a step is not at all what I would expect my compiler
to do.

Any thoughts, ideas or suggestions?
Thanks in advance!

Sincerely,
Steve

p.s. This was using GCC 4.1 haven't tried it on any others yet.



More information about the PLUG mailing list