Automatic conversion of stucture to pointer in function call

171 views Asked by At

We have a pretty normal looking printf style function in our project, with the modification that the %g format means to print a GUID instead of the normal floating-point type. For our case, a GUID looks something like this:

struct guid {
  uint32_t  Data1;
  uint16_t  Data2;
  uint16_t  Data3;
  uint8_t   Data4[8];
};

In reality, the print function expects a pointer to the GUID to be passed, as opposed to the structure itself:

struct guid myGuid = { 0x867FD1E7, 0x9AA7, 0x472A, { 0xAA, 0x56, 0xF2, 0xDA, 0x66, 0x62, 0xCD, 0x4D } };
print("%g", &myGuid);

There are several places in the source base, however, where for some reason the entire guid is passed:

print("%g", myGuid);

This style of call seems to work fine with MSVC2003 - is there some ABI requirement that makes the compiler translate that function call style to actually pass a pointer behind the scenes? When porting this codebase to use clang/llvm, it certainly doesn't do the same thing.

Can somebody explain why the second version of the call works with MSVC? A pointer to the appropriate documentation would be much appreciated!

1

There are 1 answers

0
Carl Norum On BEST ANSWER

I think I found some clarification on MSDN:

Any argument that doesn’t fit in 8 bytes, or is not 1, 2, 4, or 8 bytes, must be passed by reference.

Looks like it's time to fix clang!