I just had an epiphany today while walking down Bloor Street in Toronto: you can use CIL to learn C++ (and MC++). My epiphany came after reading some of the items in Herb Sutter’s Exceptional C++ (the chapter on Exception Safety Issues and techniques is absolutely essential reading) this afternoon.
Let’s face it, C++ compilers do an awful lot of work behind your back. Especially when templates are involved. This is why it takes a shelf full of books (that you have read ;) written by really, really smart people like Scott Meyers, Herb Sutter, and Andrei Alexandrescu to explain to you how you can shoot yourself (or avoid shooting yourself) in the foot.
So what do poor schmucks like myself, who have been programming in uh more trendy languages the past few years, do? Well, for one thing, you carve out a large chunk of time and you read the recent C++ canon:
- Effective C++ CD by Scott Meyers
- Effective STL by Scott Meyers
- The Standard C++ Library by Nicolai Josuttis
- Exceptional C++ and More Exceptional C++ by Herb Sutter
- Modern C++ by Andrei Alexandrescu
- Standard C++ IOStreams and Locales by Angelika Langer and Klaus Kreft
Consider Item 6 from Exceptional C++ on Temporary Objects (It was based on Guru of the Week (GOTW) column #2). Here is the code frag from the book:
string FindAddr( list<Employee> emps, string name )
{
for( list<Employee>::iterator i = emps.begin();
i != emps.end();
i++ )
{
if( *i == name )
{
return i->addr;
}
}
return "";
}
There are some obvious and non-obvious temporary objects that are created in this example. Now, most developers that I know would not look at the x86 code that was generated by the compiler for this rather innocuous looking function. However, what if you looked at the considerably more readable CIL?
The first place to look is at the code that is generated for calls to the FindAddr method:
string UseFindAddr()
{
list< Employee > emp;
emp.push_back( Employee( "john", "canada" ) );
return FindAddr( emp, "john" );
}
If you look at the generated CIL using ILDASM, you will be shocked by the number of custom modifiers that are generated by the MC++ compiler. The following listing shows a heavily edited disassembly that shows just the bare essentials of the return FindAddr( emp. “john” ) line of code.[1]
L_0066: ldloca.s V_11
L_0068: stloc.1
L_0069: ldloc.1
L_006a: ldsflda ?A0xe390182c.unnamed-global-3
L_006f: call (basic_string).__ctor
L_0074: stloc.s V_5
.try {
L_0076: ldloca.s V_10
L_0078: stloc.s V_4
L_007a: ldloc.s V_4
L_007c: ldloca.s V_15
L_007e: call std.list<Employee>.__ctor
L_0083: stloc.3
L_0084: leave.s L_0092
}
.fault {
L_0086: ldsfld (basic string allocator)
L_008b: ldloc.1
L_008c: call __CxxCallUnwindDtor
L_0091: endfinally
}
L_0092: ldarg.0
L_0093: ldloc.3
L_0094: ldloc.s V_5
L_0096: call FindAddr( )
L_009b: pop
L_009c: ldloc.0
L_009d: ldc.i4.1
L_009e: or
L_009f: stloc.0
L_00a0: leave.s L_00af
In this block of code, you can clearly see the calls to the constructor of the basic_string class and the copy constructor of the list< Emplyee > class. Thus, two temporaries objects are created prior to the call to the FindAddr method.
Next, let’s turn our attention to the implementation of the FindAddr() method. I won’t repeat the disassembly here because of its length (and the fact that the interesting bits are scattered throughout the code). Interested readers can see the dissasembly of FindAddr here. However, I want to point out the two interesting lines of code:
return i->addr;
and
return "";
Both of these lines result in the generation of a temporary object that is returned to the caller, as you can clearly see by the two calls to the basic_string class’s constructor.
What’s even more interesting is what happens when you compile the same code using Release compiler settings. As the disassembly of the release code shows, the calls to the basic_string class’s constructors have been optimized out of the code.
What this points out very clearly is that you should never ever be tempted to return references to temporary objects, i.e. by changing the method signature to:
string& FindAddr( list emps, string name )
This is clearly very dangerous (what happens to the “” string after the it goes out of scope?) and a pointless optimization since compilers can optimize away the creation of the temporary objects.
[1] I created this disassembly using a combination of Serge Lidin’s ILDASM and Lutz Roeder’s Reflector and Notepad. I heavily edited some of the generated names, and replaced them with abbreviated names in parenthesis. I did this to improve readability. If you want to see the original disassembly, click here. Clearly, somebody must create a better disassembler with various options that let you simplify the generated output.