C on embedded systems has traditionally use structs to hold structured data.

Arduino brings C++ to the table, so we can use classes instead.

Lets say we have two different data structures which could be considered very similar:

typedef struct 
    int valueOne;
    int valueTwo;
    int valueThree;
} value_t;


class Value
        int valueOne;
        int valueTwo;
        int valueThree;

In terms of memory, what difference will this make?

I still see the struct technique being used heavily - why is this?

  • 5
    if I recall my C++ correctly there is no difference between struct and class besides the default visibility – ratchet freak Mar 7 '14 at 12:45
  • 4
    I would assume structs are used primarily because the great majority of embedded hardware is coded in C, so anyone who spends a lot of time writing code for small MCUs probably knows C much better then C++, and automatically reaches for C idioms before C++ idioms. It's a programmer performance issue. – Connor Wolf Mar 8 '14 at 10:11

As taken from the accepted answer from When should you use a class vs a struct in C++?

The only difference between a class and a struct in C++ is that structs have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance, and can have member functions.

I would recommend using structs as plain-old-data structures without any class-like features, and using classes as aggregate data structures with private data and member functions.

Memory wise the access modifier makes no difference and given the memory constraints of the Arduino, people are less likely to use classes with complex hierarchies, but prefer the POD structs anyway.


Unlike C, an instance of a struct in C++ is an object in exactly the same way as an instance of a class. From the point-of-view of the compiled code, they are identical. Memory usage, alignment, access times etc. are exactly the same (i.e. there are no overheads).

From the programmer's point-of-view, there is a very minor difference. Members of a struct have public visibility by default, whereas members of a class have private visibility by default. Otherwise, all language features work the same on both, such as constructors/destructors, inheritance, polymorphism, templates, and operator overloading. You can even derive a struct from a class, and vice versa.

Despite the similarity, it's quite common to see people deliberately using a struct in C++ for very simple structures, e.g. where it only consists of a few data members, but no functions. A class would be used for anything more complex. This is purely a matter of convention or personal preference though, and can be used as a subtle indication of the structure's intended complexity.


As other answers have pointed out, your particular struct and class are indistinguishable performance wise (There are slight differences in the scopes of the type names, due to the way you defined your struct). The delineation in C++ is not between struct and class, but between types that are POD (plain old data) and types that are not, as explained in this discussion.


In terms of memory, what difference will this make?

None. Structs and classes are the same thing, differing only in protection levels, and instantiating either creates an 'object'.

still see the struct technique being used heavily - why is this?

Less typing if you're not trying to hide data members.


Note that

typedef struct { ... } Foo;

is obsolete in C++ and you should just use

struct Foo { ... };

It's not very clear in the answers but the other effect of structs is public inheritance by default vs private inheritance by default with classes.

As mentioned by others, using struct is generally a convention for POD types.

Also technically 'struct vs object' should be 'struct vs class' (as instantiating a struct or a class still gives an object).

  • Edited question object->class. – Cybergibbons Mar 19 '14 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.