Join waitlist
C++Community question

sizeof a reference in C++?

Asked by Rahul Yadav 1 month ago 1 answer 4 votes

Quiz said sizeof(reference) gives the size of the referred type. I guessed it right but don't understand why. Does a reference take memory or not?

1 answer

Karthik Rajan1 month agoMost voted5

sizeof on a reference gives sizeof the referred type, that's just how the standard defines it. Whether the reference itself takes memory isn't specified, compilers usually implement it as a pointer internally when it needs storage, but you can't observe that from the language.

Easiest way to think: pointer is its own object (has an address, can be null, can be reassigned), reference is just another name for an existing object.

Related questions

Go deeper

Want the full explanation, not just an answer?

Structured lessons on DSA, core CS, Java and interview skills, each ending in a quiz so you know it stuck.