The Database Managers, Inc.

Contact The Database Managers, Inc.


Use an RSS enabled news reader to read these articles.Use an RSS enabled news reader to read these articles.

Job Interview: What's Wrong With this Code?

by Curtis Krauskopf

The problem with the Gimpel Software Bug of the Month #750 (repeated in Listing 1 for your convenience) is on line 11. Carefully look at the ON_THE_R0CKS symbol. The O of ROCKS is actually a zero (0). Gimpel's PC-lint tool provides a hint to the problem by reporting that the ON_THE_ROCKS symbol is not used in the program even though it's defined.

1    #include <stdio.h>
2
3    #define ON_THE_ROCKS
4
5    const char *ice()
6    {
7    #if defined(Shaken)
8        return "shaken not stirred";
9    #elif defined(Stirred)
10       return "stirred not shaken";
11   #elif defined(ON_THE_R0CKS)
12       return "on the rocks";
13   #else
14       return "";
15   #endif
16   }
17
18   int main()
19   {
20       printf( "Celebrate the New Year " );
21       printf( "with your drink %s.\n", ice() );
22       return 0;
23   }
Listing 1: Gimpel Softare Bug Example

The zero in the ON_THE_R0CKS symbol causes the ice() function to compile with one line:


return "";

Many thanks to alert reader ZenJu for correcting my original answer.

The next code sample is actually in C#. You might think that it's not fair to use a C# code sample in a C++ interview. I, as an interviewer, would not do that but I've been in an interview where the interviewer thought that JavaScript and Java were the same thing. If you recognized that the program was in C# and said something to the interviewer in a cordial way, then points for you. Even though you might not have ever programmed in C#, the syntax is close enough that you should be able to provide constructive criticism about the code sample.

Eric Gunnerson's blog on msdn.com provides details about what's wrong with the code from a C# programmer's perspective.

Assuming that I didn't know C# and wasn't familiar with HTML, I would suspect that appending the input string to "<h1>Values</h1>" would cause the input string to be adjacent to the label. For example: ValuesLike this. This is not the case for normal HTML because the </h1> tag forces a new paragraph (caveat: this might be overridable with CSS but I'm not that much of a CSS expert to know).

The point of the first code sample was to provide an example of a program that wasn't C++ but was similar to C++ and to examine it from a C++ coder's perspective.

This is the C# code for your convenience:


enum ResponseType { ReturnValues, InvalidInput };

string CreateWebText(string userInput, ResponseType operation) {
   switch (operation) {
      case ResponseType.ReturnValues:
         userInput = "<h1>Values</h1>" + FilterOutBadStuff(userInput);
         break;
      case ResponseType.InvalidInput:
         userInput = "<h1>Invalid</h1>" + FilterOutBadStuff(userInput);
         break;
   }
   return userInput;
}

Listing 2: C# Code Example

The second code sample, shown in Listing 3, asks if a number can be assigned to a structure. The trick in Listing 3 is that the code looks like it's doing one thing when it's really doing something else. When 7 is being assigned to the Fred structure, the compiler looks for a conversion for 7 to the Fred structure. One is available with Fred's constructor. By tracing this code in C++ Builder, you'll be able to see what happens. A std::vector with 7 empty elements is instantiated. The std::vector::operator=() method copies the temporarily instantiated empty vector to f. The value that is written to cout is 0 because the temporary vector was instantiated with 7 empty elements.


struct Fred {
  Fred(int n): v(n) {}
  std::vector<int> v;
};

int main() {
  using namespace std;
  Fred f(1);
  f.v[0] = 5;
  cout << f.v[0] << endl;
  f = 7;          // is this legal?
  cout << f.v[0]; // what is output here?
}

Listing 3: Is this code legal?
Previous Answers More C++ Answers
Jump to Questions Page:  1  2  3  4  5  6  7  8  9  10  11  12  13 
 
Jump to Answers Page:  1  2  3  4  5  6  7  8  9  10  11  12  13 
Services | Programming | Contact Us | Recent Updates
Send feedback to: