They are identical; proof - I compiled and saved the assembly code generated by both MSVC 2015 and GCC 4.9.3 for these two code samples:
// Case 1: Pass by reference to single struct
typedef struct _mystruct
{
int x;
int y;
} mystruct;
void foo(mystruct *s, int count)
{
int i;
for(i = 0; i < count; i++)
{
(*(s + i)).x = 5;
(*(s + i)).y = 7;
}
}
int main()
{
mystruct ps;
//mystruct as[1];
foo(&ps, 1);
//foo(as, 1);
return 0;
}
I note that the operations in foo are random and not really relevant to the test; they are just to prevent the compiler from optimizing out the method.
// Case 2: 1-length array
typedef struct _mystruct
{
int x;
int y;
} mystruct;
void foo(mystruct *s, int count)
{
int i;
for(i = 0; i < count; i++)
{
(*(s + i)).x = 5;
(*(s + i)).y = 7;
}
}
int main()
{
//mystruct ps;
mystruct as[1];
//foo(&ps, 1);
foo(as, 1);
return 0;
}
In the generated assembly files, on GCC they are exactly identical, and in MSVC, literally the only differences are:
- The variable names in the comments (
s vs as)
- The line numbers referenced (since different ones are uncommented in each version).
Therefore, it is safe to assume that these two methods are identical.