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).
More Related Contents:
- New to programming: How to call a structure’s variables in a function and then update them? (C++) [closed]
- Simplest way to determine return type of function
- error: incompatible types for a function that returns float in C
- Matrix Printing using Pointers [closed]
- How do I compare chars (or strings) using void functions, also comparing chars that were taken from a struct array
- When to use bit-fields in C?
- How to use a C++ string in a structure when malloc()-ing the same structure?
- Where should I prefer pass-by-reference or pass-by-value?
- Top-level const doesn’t influence a function signature
- Is there a way to pass auto as an argument in C++?
- Structure of Arrays vs Array of Structures
- Difference between [square brackets] and *asterisk
- Using multiple .cpp files in c++ program?
- Is it a good practice to define C++ functions inside header files? [duplicate]
- Why use a const member function?
- Is there still a use for inline? [duplicate]
- Explanation of function pointers
- Casting double array to a struct of doubles
- Declare variables at top of function or in separate scopes?
- What is the difference between using a struct with two fields and a pair?
- Get attribute by name
- c++ sort with structs
- Mixing class and struct
- When is C++ covariance the best solution?
- Pointer to array of unspecified size “(*p)[]” illegal in C++ but legal in C
- About Pointers To Functions in function declarations
- What happens when a function that returns an object ends without a return statement
- array of pointers as function parameter
- Can I treat a specific warning as an error?
- Why can’t C++11 move a noncopyable functor to a std::function?