Does the reference consider as pointer in c++ ?
int &x = y;
Does x have an space in the memory ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Implementation-dependent and not specified by the standard. The compiler may treat
int &xas if it were a pointer, and make space for it on the stack; it may hang onto it in a CPU register (and thus not need stack space); it may realize that it’s a different name for an existing object and just conflate the two at runtime.Here’s a couple of situations as an example of how the compiler might do different things with a reference depending on how it’s used.
In a function like:
A compiler like MSVC will simply consider y as an alias for x — a different name for the same variable — and quietly replace all mentions of y with x during compilation. Thus the function will actually be compiled as if it were
But, if you use a reference as a parameter
Then x is internally implemented as a pointer and passed into the function on the stack like any other parameter. The compiler treats it as if it were:
The point is that the answer to your question depends on not just which compiler you are using, but also the particular code you’re writing. It’s not specified by the standard in any way.