Posts Tagged ‘c’

C++ Round-Up

Sunday, March 15th, 2009

Here’s a round-up of interesting links I’ve recently come across related to C++:

dcc – A decompiler that outputs C code. It’s very restricted and the derivative project, Boomerang, appears to be abandoned. It’s hard to see how this could replace OllyDbg, but it’s an interesting piece of work.

Boost Memory Mapped Files – Boost has been solving core problems of C++ for years in a cross-platform way. I’m glad to have finally noticed this addition.

chSoftIntegration has a product that is a scripting language with C++ syntax. According to their web page, the scripting engine can read C++ source files. The idea of code re-use for scripting is interesting, but I suspect shortcomings exist. Also, with all the modules for Perl at CPAN, it’s hard to see the value.

Spec – An interesting idea of embedding specifications into C++ in a readable manner to support Behavior-Driven Development (BDD).


How Large Is a Short, Int, Long or Long Long?

Sunday, February 1st, 2009

How many bytes are in an int or a long? Most think int type sizes vary with platform between 2 or 4 bytes while the long integer is 4 bytes regardless. Though that can happen, that’s not how the C standard defines int type sizes.

The C standard allows the int types to vary as everyone expects, but C only defines a minimum size. The short integer must have at least 16 bits. Type int must also have at least 16 bits and be at least as large as the short integer. The long has a minimum of 32 bits. C99 introduced the long long type which has a minimum of 64 bits. For each type, the C Standard defines no upper boundary. Here’s a quote from the C99 draft standard:

The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. […] Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

The previous passage is talking specifically about the definitions in limits.h, which, in the standard, have the following definitions:

minimum value for type short int: SHRT_MIN -32767 // −(152−1)
maximum value for type short int: SHRT_MAX +32767 // 152−1
maximum value for type unsigned short int: USHRT_MAX 65535// 162−1
minimum value for type int: INT_MIN -32767 // −(152−1)
maximum value for type int: INT_MAX +32767 // 15−1
maximum value for type unsigned int: UINT_MAX 65535 // 162−1
minimum value for type long int: LONG_MIN -2147483647 // −(312−1)
maximum value for type long int: LONG_MAX +2147483647 // 312−1
maximum value for type unsigned long int: ULONG_MAX 4294967295 // 322−1
minimum value for type long long int: LLONG_MIN -9223372036854775807 // −(632−1)
maximum value for type long long int: LLONG_MAX +9223372036854775807 // 632−1
maximum value for type unsigned long long int: ULLONG_MAX 18446744073709551615 // 642−1

The key point is the C standard defines minimum values and your platform may have different values than what the standard states, but if so, the values must be larger. The safe way to determine the minimum and maximum values for all C types on your platforms is to use limits.h, e.g.:

#include <limits.h>
printf("Minimum value for a signed int: %d\n", INT_MIN);
printf("Maximum value for a signed int: %d\n", INT_MAX);

In C++, rely on limits rather than climits. In C++, limits defines a template class, numeric_limits, specialized for each of the fundamental types (e.g. int, short, float, double, etc.). To determine the range of possible values in C++ using numeric_limits, for an int, you would do the following:

#include <limits>
#include <iostream>
using namespace std;
cout << "Minimum value for a signed int: " << numeric_limits<int>::min() << endl;
cout << "Maximum value for a signed int: " << numeric_limits<int>::max() << endl;

Good references on the topic are Harbison and Steele’s C A Reference Manual and Steve Summit’s C FAQs.


Correct Use of size_t

Saturday, January 31st, 2009

One misunderstood part of C is size_t. First appearing in ANSI C around 1990, size_t is routinely misused. People either use size_t incorrectly or they don’t don’t use it at all. The idea of its use is simple and I’ll attempt an equally simple explanation. The appropriate use for size_t is to describe the size of objects and not as an alias for unsigned int.

unsigned long min_index=min, max_index=max;
for (size_t i=min_index; i++) data_array[i]++;

The previous code can break. The assumption in the previous code sample is that size_t can store min_index and max_index, but on some platforms, size_t may be smaller than an unsigned long, causing overflow errors. The above code should use unsigned long for the index.

Using size_t as an alias for some type of int on your target architectures may seem functionally sound, but your assumptions are likely wrong and the code is semantically wrong. Since the type of size_t will shift from platform to platform, you shouldn’t rely on size_t to do anything other than describe the size of objects and types.

int obj_size = get_obj_size();
struct very_large_struct *ptr_src_vl=src, *ptr_dest_vl=dest;
ptr_src_vl = malloc(obj_size);
/* sanity checking for ptr_src_vl validity not shown */
memcpy(ptr_src_vl, ptr_dest_vl, obj_size);

The previous code is not using size_t where it should. The C standard states int types must be at least 16 bits. Therefore, the above code has an implicit limit through the size of the int type. Imagine size_t is 16-bits and has a sign (it can happen, but shouldn’t) yielding a maximum value of 32,767. The lesson is: the maximum value for an int type has no bearing on the maximum object size for a platform. However,  size_t will hold the maximum possible object size for your system (assuming your C implementation is correct). If you go with the C standard on this issue, it will help your code portability and maintainability.


Interviewing Programmers

Sunday, January 25th, 2009

After my former CEO said, “We’re shutting the company down,” I had to interview. Which is not much fun when the interviewers aren’t good at it. Bad interviewers, about half of those whom I met, did one or more of the following mistakes:

  • Assumed candidates are perfect fits, until proven otherwise
  • Relied on random quiz questions to establish technical competence
  • Looked for a grocery list of skills rather than intelligence or ability
  • Forgot the candidate is also interviewing them

Start at 0 and Work Up

In most interviews, I walk in the door as a “100%-fit” in the eyes of the interviewer and lost percentage points for every question I missed. For knowledge-intensive workers, it’s the wrong approach. At one interview, I aced it after correctly answering all their questions. The job wasn’t a good fit for me, but because I had the right answers, I was a fit. However, the job required skills I didn’t have — they just hadn’t found a reason to not hire me. Failing to find reasons to not hire is not the same, or as good as, finding reasons to hire. Find reasons to hire candidates rather than assuming they will work until they miss a question. This removes luck – some can sail through interviews and still not be a match.

Quiz Questions

The technical interview usually unfolds as a quiz of technical questions, that are often obscure. The logic is, if the candidate knows the obscure parts of a technology, they must know the common parts. At one interview, I aced the quiz and managed to get bonus points by answering the question, “How do you share file descriptors between Unix processes?” Coincidentally, the week before, I read the solution in W. Richard Stevens‘s Advanced Programming in the Unix Environment. They over-rated my ability based on trivia questions I happened to get right. Joel Spolsky wrote a significant article for hiring technical workers, The Guerrilla Guide to Interviewing and in it, said the following about interviewers employing quiz questions:

This is the kind of person who thinks that smart means “knows a lot of facts.” […] There is no possible, imaginable correlation between people that know that particular piece of trivia and people that you want to hire.

He’s right. However, candidates need to enter with a minimum of knowledge. Phone screens are good for determining if the requisite knowledge is present. For C/C++ programmers, I ask basic knowledge-based questions on fundamental aspects of the language. What is the stack and the heap? What is the time complexity for an algorithm that traverses a binary-tree? Explain inheritance and polymorphism. This is not obscure trivia, or a quiz, its a sampling of the basics you need to know to be effective in C++. That and some descriptions of how they contributed in recent projects will fill out phone screens nicely.

Match Ability Before Skills

Looking for specific skills or experiences can exclude great candidates. Sometimes, you can spot a match for another group or, if you have open mind, you can spot abilities that can overcome skill gaps. Imagine your open position requires a C++ network programmer, though the candidate knows networking programming, it’s with Python and Perl. The person demonstrates amazing knowledge of algorithms, fantastic problem-solving skills and a great attitude that would fit in with your team. Moreover, the candidate is self-taught in both Python and Perl, becoming adept in each in weeks. Given a choice between someone with high intelligence and gaps in skill and another with average intelligence and no gaps, with all else being equal, I’ll select the more intelligent candidate.

Forgot the Interview Goes Both Ways

In one interview, I was made to wait 30 minutes as the hiring manager “had an important meeting.” It’s hard not to read into the manager’s priorities. Worse, when my prospective manager showed up, it was clear he had not read my resume. When he became bored with my answers, he would interrupt to start with the next question. An interview is like a first date where everyone is on their best behavior. If the prospective employer is rude during the interview, it’s hard for candidates to imagine the workplace values workers and their contributions. Maybe the manager had a legitimate emergency and was “off their game.” The best thing to do would have been to apologize and re-schedule the interview. Though that’s also poor, it is better than overwhelming potential employees with unprofessional behavior. Even if the interviewees are poor fits, it’s best to treat them with the same respect as everyone else because it’s the right thing to do.


  1. Screen the prospect, ensure they have the minimum knowledge, skill set and experience.
  2. Figure out what the candidate can do.
  3. Avoid quiz questions, focus on relevant problem-solving.
  4. Don’t just match positions with skills, look at the candidate’s abilities.
  5. Remember, the candidate in interviewing you, too.