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

Bug Buster 004: Can You Bust the Bug?

This is our fourth bug for you to track down and eradicate. You can find the previous bug here! How good are you at finding problems in code? The following is a code snippet compiles fine. Unfortunately, it is believed to have a problem. Does it? Take a look at the code listing and see […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 8, 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

This is our fourth bug for you to track down and eradicate. You can find the previous bug here! How good are you at finding problems in code? The following is a code snippet compiles fine. Unfortunately, it is believed to have a problem. Does it? Take a look at the code listing and see if you can determine which of the statements that follows it is correct. One of the options is correct, while the others are not.

Can you find any issues without looking at the choices?

Bug Buster #4 

Here is the Bug Buster:

Code:

#include <iostream>
using std::cout;
using std::endl;

struct B {
  int virtual f() { return 1; }
};

struct D : public B {
  int virtual f() { return 2; }
};

void main() {
	B* pd2 = new B;
	D* pb2 = dynamic_cast<D*>(pd2);

	cout << "result is " << (pb2 ? pb2->f() : 0) << endl;

	char x;
	x = 1;
}

Your choices:

The listing will throw an exception when trying to access pb2->f().
The listing will throw a runtime exception when pb2 is declared.
The listing seems to work; however, the result is 2 will be displayed.
The listing seems to work; however, the result is 1 will be displayed.
The listing seems to work; however, the result is 0 will be displayed.
The listing seems to work; however, a memory address will be displayed.

How quickly can you find the answer? Feel free to post a comment on how quick you were! We will be posting additional snippets over the coming weeks with additional coding issues 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.