Something that I have discovered over the years is a great pleasure. When I am giving information - presenting, teaching, writing an article or a blog - it is not necessarily a one-way process. I often receive useful and interesting information back. I have commented that I learn as much from delivering a class as I might from attending one.
I was recently talking about C++ for embedded at a conference and considering some of the features that I felt resulted in better, clearer and more bug-free code. One of these was the option to pass parameters by reference. Then someone explained a drawback of this feature …
Pointers are a very powerful feature of the C language and largely explain its wide usage for embedded programming. But they can also be confusing and error prone. For example, consider this code:
int *p; p = (int *)0x8000; p += 1; // this could equally be p=p+1 or p++
What is the final value of p?
Assuming the compiler in use regards integers as 32 bits, the answer is 0×8004.
Although, to the experienced programmer, this may be crystal clear, it is far from obvious to someone coming to the language for the first time.
Of course, C++ has pointers too and this example behaves in the same way, but C++ has some opportunities to avoid the use of pointers and, hence, write clearer code. And one of those is the option to pass parameters by reference.
This code might be written in C, for example:
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
The parameters are pointers to data in the calling function and this code is littered with pointer operations, every one of which is a potential error. So, in C++ we can pass the parameters by reference, thus:
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
Now, the calling function does not need to provide [explicit] pointers and the pointer operations are eliminated in the swap() function, rendering it more readable and less likely to contain a bug.
This is all fine, but the problem that was pointed out to me was with a call to swap(). In C, the call would look like this:
swap(&x, &y);
It is 100% clear that pointers have been passed and, hence, the possibility that the values of x and y might be changed by the call to swap() is quite clear. However, in C++, if reference parameters were used, the call would be written like this:
swap(x, y);
And there is no clear indication, without resort to the function prototype [which would, of course, be readily on hand if you are using a good IDE], that x and y could get changed.
I guess it is a case of “what you win on the swings, you lose on the roundabouts”.
Preparing Recommendations
Comments (↓ Add Your Own)
8 Comments on this Post
Commented on 8:47 PM, Feb 23, 2011
By Gene Bushuyev
Commented on 4:50 PM, Feb 24, 2011
By Dan
Commented on 11:44 AM, Feb 25, 2011
By Colin Walls
Commented on 1:59 PM, Mar 30, 2011
By Matthew Limber
Commented on 8:43 AM, Mar 31, 2011
By Colin Walls
Commented on 5:50 PM, Mar 31, 2011
By Matthew Limber
Commented on 4:46 PM, Apr 1, 2011
By Matthew Limber
Commented on 4:48 PM, Apr 1, 2011
By Matthew Limber
Add Your Comment
Please complete the following information to comment or sign in.