Bug Buster 001: Can You Bust the Bug?

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Bug Buster

Sponsored by Rogue Wave, maker of Klocwork

How good are you at finding bugs in code? The following is a code snippet that contains an error. Take a look at the code listing and see if you can find the error. To make it easier, we’ve listed five options. One of those options is correct while the other four are not.

Can you find the error without looking at the choices?

Bug Buster #001

Here is the first Bug Buster:

The code:

char letters_array[26];
for( int i = 0; i <= 26; i++ )
          letters_array[i] = (char)(i + 64);

cout << letters_array[1] << letters_array[18];
cout << letters_array[3] << letters_array[3];

Your choices:

The char cast in the third line causes a type-mismatch error. You can’t do (char) (i + 64)
The for loop is infinite.
There is a buffer overflow for the letters_array array.
letters_array is not initialized before it is used.
Nothing is wrong. The code works fine.

 

How quickly can you find the issue? Feel free to comment on how quickly you found the issue! We will be posting additional snippets over the coming weeks with additional bugs for you to bust. You can click to the next page to find the answer.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read