Complete, Concrete, Concise » Entries tagged with "c++"
Programming Error – Unintended String Concatenation
In an effort to be helpful, C and C++ compilers concatenate adjacent strings together. In the following example, the compiler will concatenate the two strings into a single string: printf("This is a string " "and this is another string."); Which is probably what you intended anyway. Because C and C++ are free form languages, adjacent strings can appear in many different formats – as this string table shows: char *array[] = { "str_1", "str_2", "str_3" "str_4", "str_5", NULL}; Did you notice the missing comma (,) after "str_3"? It can be a hard one to spot. This is the type of bug that can result in something like a simple parser failing: if (strcmp(array[i], test_string) == 0) because str_3 and str_4 will … Read entire article »
Keyword – switch, case, default
The switch keyword is probably the least well understood of the C/C++ language keywords (although, const probably comes a close second). The keywords switch, case, and default always go together and cannot be used independently in any other context. There is a small difference in behaviour between C and C++. Most programmers will never notice the difference. The most common use of switch is as a replacement for multiple if-else statements: switch (value) … Read entire article »
Debugging – An Easy Way to Catch Typing Errors in C++
A common bug in C++ programming is mixing different data types together – generally, this is inadvertent. I developed this technique back in 1997 or 1998 after my recommendation that all types be made classes was shot down. It requires your programming style does not use the fundamental C++ data types directly, but instead you rename them using a typedef. You follow good programming practice and don’t use fundamental C++ types directly, but hide them behind a typedef. Unfortunately, typedef only gives it a new name, but does not create a new type. typedef float TEMPERATURE_C; typedef float TEMPERATURE_F; typedef float WIND_SPEED_MPH; typedef float WIND_SPEED_KMH; typedef float BAROMETRIC_PRESSURE_KPA; typedef float BAROMETRIC_PRESSURE_MILLIBARS; While it makes for code that is more self documenting: TEMPERATURE_C GetCurrentTemperature(void) { return g_current_temperature; } it does not change the fact that all the new types are really … Read entire article »
Filed under: C++
Compilation Error – Improperly Defined Macro
Macros are tricky things to use in C/C++ programming. When they work, they work great. When they have bugs, they are a pain to troubleshoot. Macros are a simple text substitution done by the preprocessor. Whenever the macro name is encountered in your code, the preprocessor replaces it with the text to the right of the macro name (plugging in any optional parameters you may have passed). A macro is composed of three parts: #define – this introduces the macro macro name and optional parameter list. The macro name cannot have any spaces and the parameter list cannot be separated from the macro name with any spaces. text that will replace the macro – everything following the macro name (and optional parameter list), until the end of the line, is what the preprocessor will use … Read entire article »
Programming Error – Incorrect Array Declaration
In C++, we can declare an array by using the new operator: char *p = new char[50]; // dynamically allocate an array of 50 char This is useful in functions when we don’t know in advance how large the array should be or if we want to allocate the array in the heap instead of the stack (the equivalent in C would be to use malloc() or calloc() to allocate the array) A common programming error (because it looks very similar and is perfectly valid) is to replace the [] with (). char *p = new char(50); In this case, we have allocated a single char and initialized its value to 50 – which is not what was intended. This leads to those hard to track down bugs of corrupted data and / or random … Read entire article »
Filed under: C++
Recent Comments