Archive for the ‘C/C++’ Category

The C/C++ Programming languages obsolete?

February 6, 2010

Recently I have heard opinions of some developers
“There is no big loss that one haven’t programmed in C/C++”
“C is useless now days, there is no point of learning it”

Nothing could more far from the truth.
I would like to point out that I am 28 year old and consider myself young. Despite of this I am glad that I do know C/C++ and have worked extensively with these languages. Knowledge of the C and C++ have given me deep understanding of how computer programming works in general and even now that I do not use them any more I am still applying most of the principles on daily basis.

It is important to understand that every programming language that you know gives you new view of things and affects your way of thinking if you aren’t actually using it. It would be best if every developer knows C, one functional language, one scripting language.

So you are C# or Java developer and you think you do not need C/C++ because they are “useless”
Can you answer following questions:
– In what language is written the operating system that you are using ( be it Windows, Linux, BSD, Solaris)
– In what language is written the C#/Java runtime
What about the database, the web server?
I could go on and on but you I think you see my point.

C/C++ is used in almost everything you use and probably will never be obsolete. Even if you don’t use it you actually NEED to learn it ( and not just C/C++) in order to be a good developer no matter what language you are currently programming in.

memccpy considered harmful ( if you don’t know how to use it)

February 26, 2008

Ever heard of the memccpy function?

It is very similar to memcpy, but it will stop copying if given delimiter character is encountered. So that’s a nice feature – let’s just use it instead of memcpy everywhere!

Except that you must pay extreme attention to what are you actually copying, and what kind of delimiter character are you using. Copying text strings and using line feed ‘n’ as delimiter is fine, but copying mac addresses, and using ‘n’ as delimiter, as a coworker did, is not.
But why, you may ask. Well because the ‘n’ character, 0x0a in hex, is pretty valid byte to come in a mac address. And guess what – if you use memccpy to copy a mac address and use ‘n’ as delimiter, it is going the get truncated! And because the target buffer is already allocated with certain size and filled with some random data, that may also be valid for mac address, this kind of error may go unnoticed for a long time.

This brings up the question of the data representation, that you are using and how well you understand it, and also the quality of the interface definitions of common C functions and stuff like:

if(strcmp(str1, str2)) {
/* if you expect to get here
if str1 and str2 are the same,
you are in for a surprise

Otherwise remember it also very important to always check the return values, that you are getting and event more important also: test, test, test (more…)