C++ for Embedded · Interview question

How should you pass a large object to a function, and why?

A strong answer

By const reference, void f(const BigThing& x). Passing by value would copy the entire object on every call, which for something like a 256-byte sensor frame is real, repeated cost on an MCU. A const& passes an alias (effectively a pointer under the hood), so it's cheap regardless of size, and the const guarantees the function can't modify the caller's object, giving you the no-copy benefit without giving up safety. If the function needs to modify the caller's object, pass a non-const reference (BigThing&) as an out-parameter. Only small, cheap-to-copy types (int, float, a pointer, a small POD) should be passed by value, where a copy is as cheap as passing an address and avoids a layer of indirection.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

References vs Pointers

References are aliases that can't be null and can't be reseated. When to reach for a reference, when you still need a pointer, and why const& is the default way to pass objects.

More References vs Pointers questions

Browse all 472 interview questions
How should you pass a large object to a function, and why? | EmbeddedPrep.io