Bug Buster 003: Can You Bust the Bug? | CodeGuru

Bug Buster 003: Can You Bust the Bug?

This is our third bug for you to track down and eradicate. You can find the first bug here! How good are you at finding bugs in code? The following is a code snippet compiles fine. Unfortunately, it contains an error. Take a look at the code listing and see if you can find the […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 22, 2015
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Bug Buster

This is our third bug for you to track down and eradicate. You can find the first bug here! How good are you at finding bugs in code? The following is a code snippet compiles fine. Unfortunately, it contains an error. Take a look at the code listing and see if you can find the error. To make it easier, several options are listed. One of the options is correct, while the others are not.

Can you find the error without looking at the choices?

Bug Buster #3

Here is the Bug Buster:

The code:

#include <algorithm>
#include <vector>
#include <array>
#include <iostream>

int main()
{
   std::array<int, 10> input =
   { 1, 2, 3, 3, 5,
     6, 7, 8, 9, 10 }; // 'random' input

   // copy odd numbers in new vector
   std::vector<int> odds(5); // new container for odd numbers
   auto it = std::copy_if(input.begin(),
      input.end(),
      odds.begin(),
      /*copy odd nbrs */   [](int i) { return i % 2; });

   // shrink container:
   odds.resize(std::distance(odds.begin(), it));

   for (const auto& x : odds)
      std::cout << x << std::endl;

   return 0;
}

Your choices:

The input array will throw an exception due to the duplicate data being added.
You can’t use a mod operator (%) in the embedded return statement.
This code will throw exception on the odds vector when the resize is attempted.
The odds array will throw an exception when the numbers are being added.
The program runs fine, but the odds array will end up with even numbers.
Nothing is wrong. The code works fine.

How quickly can you find the issue? Feel free to comment on how quick you were! 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 solution.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.