Is it safe to return a struct in C or C++?

It’s perfectly safe, and it’s not wrong to do so. Also: it does not vary by compiler.

Usually, when (like your example) your struct is not too big I would argue that this approach is even better than returning a malloc’ed structure (malloc is an expensive operation).

Leave a Comment