My advisor presented me with an interesting problem the other day. Calling some simple C++ free functions that took C++ objects by value using Python's ctypes produced strange results. We eventually narrowed the test case down to the following:

1#include <stdio.h>
3struct Simple {
4 int x,y,z;
7struct Fancy {
8 int x,y,z;
9 ~Fancy() {
10 printf("Destroying a Fancy\n");
11 }
14extern "C" {
15 void printSimple(Simple s) {
16 fprintf(stderr, "Entering printSimple\n");
17 fprintf(stderr, "x=%d, y=%d, z=%d\n", s.x, s.y, s.z);
18 fprintf(stderr, "Exiting printSimple\n");
19 }
21 void printFancy(Fancy f) {
22 fprintf(stderr, "Entering printFancy\n");
23 fprintf(stderr, "x=%d, y=%d, z=%d\n", f.x, f.y, f.z);
24 fprintf(stderr, "Exiting printFancy\n");
25 }

This code sample exports the two interesting functions with unmangled names to make calling it from Python easier. The two structs are structurally identical. Allocating these in Python is easy enough using ctypes. Calling the first one with a structure by-value works as expected. Calling the second prints out nonsense. However, calling the second with a pointer to a struct works just fine. The raw x86_64 assembly is a bit hard to read, but a dump of the LLVM IR for this code is slightly more enlightening:

1define void @printSimple(%"struct.<anonymous namespace>::Fancy"* byval %s) {
2; ...
3 ret void
6define void @printFancy(%"struct.<anonymous namespace>::Fancy"* %f) {
7; ...
8 ret void

Clearly, the second function must be called with the parameter passed by pointer. But why? We figured that it must be some kind of calling convention issue. It looks like the relevant standard is the Itanium C++ ABI (Section 3.1.1). Nobody uses Itanium anymore, but g++ uses this ABI for most (all?) platforms that do not define their own (including i386 and x86_64). Basically, if a C++ object has any non-trivial constructor or destructor, the caller allocates the copy of the object being passed by value. When making the call it has to pass the address of this local temporary to the callee. Presumably this simplifies exception handling and unwinding. This was a bit surprising and adds a whole new set of complexities to calling C++ from other languages.