0

There are already several questions where people ask about the differences between classes and structs in C++. The most cited difference would be that the default visibility is different. Even Stroustrup seemed to imply that there is basically no difference.

I consider that my question is not a duplicate because I would like to ask something specific and punctual: Does the use of structs against classes have any impact in terms of speed, memory usage or efficiency? Can preferring one over the other be based on any advantage of those?

For reference, I am evaluating a person's C++ code that uses exclusively structs and I am going to port it to an ARM architecture with some limited resources.

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 4
    Except visibility, (and name mangling issue with old msvc), there are no differences. – Jarod42 Apr 26 '21 at 14:40
  • 3
    You answered your own question; "Even Stroustrup seemed to imply that there is basically no difference.". No, a struct and a class are exactly the same with the exception of the default visibility for members. – Bas Groothedde Apr 26 '21 at 14:40
  • The default visibility is the most cited difference because it is the only one. – molbdnilo Apr 26 '21 at 14:43
  • 2
    it is a duplicate, because having no difference apart from default access does include not having a difference in performance. Maybe its not stated clearly [here](https://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c), but C++ does not have structs and classes. C++ has classes. `struct` and `class` are two keywords that you can use to declare a class. I'd vote to close as dupe, but don't want to hammer it... – 463035818_is_not_an_ai Apr 26 '21 at 14:48
  • Why would a C++ compiler, whose job would be to translate the written source code into object code, and given that `struct` and `class` are the same thing, would generate different code? That would make no sense whatsoever. If the platform you're working on *did* generate different code, then contact the compiler vendor and report a bug. – PaulMcKenzie Apr 26 '21 at 14:49
  • I personally use `struct` for PODs with only public data members and no member functions; other than that, yeah, no difference. – Casey Apr 26 '21 at 15:04

1 Answers1

6

Does the use of structs against classes have any impact in terms of speed, memory usage or efficiency?

No. There is no impact. Not even compile time differences.

Also, if you want to be sure of the performance impact of something, you should measure, and benchmark. If you don't, you won't ever know. Asking on StackOverflow does not replace measuring your own program.

Note that creating a correct benchmark that measure the right things take skills, just as writing good unit tests that helps you finding bugs without slowing you down.

You can also look at the assembly output of your compiler. As for me I definitely know there is no difference between the two because I observed no difference in the assembly output between the two, in all the cases I tried.

If there is a performance and memory usage impact, you should report the bug to your compiler implementer.


As you might know by now, struct and class are defined as being exactly the same, beside visibility. The sizeof of a class is the same as the size of a struct with the same members. You can even have basic inheritance without overhead as compared to containing the type that would serve as a base. For example, std::tuple has no overhead compared to a struct but cannot be implemented without inheritance.

However, as soon as you add a single virtual function or virtual inheritance, the compiler will add a vtable and RTTI. This comes as a pointer in your type that refer to that metadata. The vtable contains all the metadata to call virtual functions, check downcasting and sidecasting, virtual base location and type id.

So when to use struct and classes is entirely personal preference. For example I use struct everywhere, because inheriting is public by default, and I put public members first.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141