0

I have a C struct with associated functions:

struct StructC {
    int a;
    int b;
};

static inline StructC func1()
{
    StructC ret;

    //do something with variables ret.a and ret.b

    return ret;
}

static inline void func2(StructC var)
{
   //do something with variables var.a and var.b
   ...
}

and a C++ struct:

struct StructCPP {
    int a;
    int b;

    StructCPP func3()        //can also be static
    {
        StructCPP ret;
        //do something with instance variables ret.a and ret.b

        return ret;
    }

    void func4()         //can't use static here since it operates on the instance variables
    {
        //do something with instance variables a and b
        ...
    }
};

My question: Which is faster when passing these structs to functions/methods?

Since the C functions operating on StructC are static, only one instance reside in memory, but what happens with the C++ methods in its struct?

Do its methods (func3() and func4()) occupy redundant memory for every instance, or does the C++ compiler optimize it to hold only one instance, so while passing the C++ struct, only the instance variables, a and b, are passed?

which function call to these functions is faster (if there is any difference)?:

void functionA(StructC var);      //StructC as argument
void functionB(StructCPP var);    //StructCPP as argument

(The program is a mixture of C and C++)

7
  • 5
    If I'm not mistaken, I believe structs are interpreted as classes in C++. Commented Feb 12, 2014 at 16:30
  • You pass objects, not structs. The methods associated with a struct do not get copied. If that's what you're asking. Commented Feb 12, 2014 at 16:32
  • 1
    With respect to performance, always test before you ask. Did you try and see if there's any difference here? Additionally, I think you are fundamentally incorrect in your understanding of structs. Commented Feb 12, 2014 at 16:32
  • If you are compiling both StructC and StructCPP with a C++ compiler, odds are that they are both 'c++ structs' Commented Feb 12, 2014 at 16:33
  • Also, there is nothing stopping you from declaring func3 as static within StructCPP since it does not access any of the member variables a or b. Commented Feb 12, 2014 at 16:34

3 Answers 3

3

Which is faster when passing these structs to functions/methods?

A member function should be exactly as fast to call as a non-member taking a pointer as an argument; since that's exactly what a (non-virtual, non-static) member function is.

The first non-member function is probably slightly faster to call than the first member, since it doesn't take a hidden this parameter. However,it doesn't access the object it's called on, so it could be static or a non-member; in which case it will be exactly as fast as the non-member.

The second takes its hidden parameter as a pointer, so it might be slower or faster than the non-member function taking a value, depending on exactly what it's doing with it.

Since the C structs are static, only one instance reside in memory, but what happens with the C++ structs?

C structs aren't static. You can create and destroy them just like any other object - as your example does when it creates a local variable, then returns a copy of it. C++ classes are just the same in that regard.

Do its methods (func3() and func4()) occupy redundant memory for every instance

No, member functions don't occupy any memory in the class instance. Like non-member functions, the code exists in just one place; the only real difference is that member functions are passed an extra argument.

If the class has virtual functions, then that (typically) adds a single pointer, the vptr, to each object, along with a single static table of function pointers and other runtime type information (the vtable) for the class.

while passing the C++ struct, only the instance variables, a and b, are passed?

Indeed. This is a standard layout class, which means that the only things it contains are its data members, just like a C struct.

which function call to these functions is faster (if there is any difference)?

They should be the same; both pass a trivially copyable object containing the same data members by value.

Sign up to request clarification or add additional context in comments.

5 Comments

I've declared the C structs as static.I've modified my question to reflect my original intention.
@Alterecho: No, you've declared the functions as static. Your example has two non-static objects of the struct type. (And I've updated the first paragraph to address your changes).
So there is no performance difference when passing objects of StructC and StructCPP ?
@Alterecho: That's right. Both have the same data members, and the same (trivial) copy semantics, so both are passed by value in exactly the same way.
Thanks. I was of the impression that the size of the objects would be increased for the StructCPP, because of the member functions.
2

Code for methods of a C++ class or struct (it's exactly the same) is only included once in your executable file and only present once in memory. It doesn't matter how many objects you create or how many times you call them¹.

The only difference between a method and a free function is that the method gets an additional "hidden" argument in the form of this. So not even the instance variables are passed individually.


¹ Declaring methods inline (or equivalently, defining them inside the class definition) might result in multiple copies of the same code being included in your final executable. This behavior is in general very compiler-specific, and in any case also applies to inline free functions.

4 Comments

And also notice in the above example: if the member func3 is static, it shouldn't even have that extra-parameters and thus make absolutely no performance difference.
@ComicSansMS: True, even though inline is not a guarantee but only a hint to the compiler. To be frank I mostly disregarded the code and went on to answer in a general sense. I 'll get to fixing that.
@DavidKernin: Even for nonstatic methods the performance difference in the call itself is absolutely not measurable. It might not even exist, e.g. if the calling convention and compiler optimizations end up making one or more arguments be passed through CPU registers.
I agree with the above.
0

The only thing that you can say for sure, considering the language specs, is the following: struct, class and union are the only 3 class types in C++, that's where the similarity between struct and class comes from.

For everything else you should profile, debug and compare different implementations and compilers, after all C++ is still a compiled language, and not everything can be said about a given program if you don't compile it.

3 Comments

The StructC, in my case, is a C struct (used in C code as well).
@Alterecho why ? what's the difference with a c++ struct ? There is no such difference, the same struct in C++ is C++ code .
Suppose i'm importing StructC from another C only program?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.