Size of a reference?

N

newbiecpp

C++ standard says that a reference is not a object. My question is a
reference has size?

int myInt;
int& myRef = myInt;

myRef is just another name of myInt? My confusion is that if myRef is not
an object, where is myRef reside in memory? Compiler has to keep
information about myRef, but where is this information?

Sorry for my ignorance. Thanks in advance.
 
V

Victor Bazarov

newbiecpp said:
C++ standard says that a reference is not a object. My question is a
reference has size?
Unspecified.


int myInt;
int& myRef = myInt;

myRef is just another name of myInt?
Yes.

My confusion is that if myRef is not
an object, where is myRef reside in memory?

Unspecified. There is no requirement that it does.
Compiler has to keep
information about myRef, but where is this information?

Somewhere in the compiler's tables. That information doesn't need to
exist after your program is compiled.

Victor
 
C

Carlos Martinez Garcia

References in some way are like pointers, but with a simpler notation.

I have read the standard about that, but I suppose it behaves like
sizeof(<some_pointer>)

For example in a 32bit platform that would be 4 (32 bits).

newbiecpp escribió:
 
R

Richard Herring

Carlos Martinez said:
newbiecpp escribió:
References in some way are like pointers, but with a simpler notation.

No. References in some ways are exactly like the thing they refer to. If
the compiler _knows_ what they refer to, it can generate the exact same
code when a reference is used as when the thing it refers to is used. So
in that case the fact of using a reference adds nothing to the size or
the execution time of the program. At runtime the reference doesn't
exist, so it has no size.

If the compiler _doesn't_ know what is referred to (e.g. it's a
parameter passed by reference to a function in a different translation
unit), then, yes, the compiler has to pass something that will identify
the object, so behind the scenes it may use something like a pointer,
but there's no requirement that it's a pointer, and no way a conforming
program can tell.
I have read the standard about that, but I suppose it behaves like
sizeof(<some_pointer>)

For example in a 32bit platform that would be 4 (32 bits).
That's just conjecture.
 
J

josh

newbiecpp said:
C++ standard says that a reference is not a object. My question is a
reference has size?

int myInt;
int& myRef = myInt;

myRef is just another name of myInt? My confusion is that if myRef is not
an object, where is myRef reside in memory? Compiler has to keep
information about myRef, but where is this information?

References are pointers that are always dereferenced. (except when
initializing them, of course) "Another name for" is not a viable
implementation in many of the places that references can be used, but
conceptually the behavior is identical so it is a convenient simplification.

int a;
int * const b = &a;
int &c = a;
// c is identical to (*b)

sizeof(c) == sizeof(*b) == sizeof(a);
because you're not measuring the size of the reference. To do that, you
have to do something like this:

struct B
{
int * const b;
B(int &a) : b(&a) { }
};

struct C
{
int &c;
C(int &a) : c(a) { }
};

sizeof(B) and sizeof(C) will almost certainly be the same. (afaik,
implementations are free to choose a different pointer representation,
but I can't imagine why that would be a good idea.)

Also, with optimizing compilers, any given variable may not reside in
memory at all. They may only exist in registers, or may even be rolled
into compile-time (or link-time) constants. This is especially true for
variables whose direct content is constant, like references.

-josh
 
S

Siemel Naran

josh said:
Also, with optimizing compilers, any given variable may not reside in
memory at all. They may only exist in registers, or may even be rolled
into compile-time (or link-time) constants. This is especially true for
variables whose direct content is constant, like references.

But in the common case of a function receiving arguments by reference, even
optimizing compilers can't optimize away the reference itself (except in
rare theoretical cases).

There's no way to get sizeof(reference type) by writing a computer program.
But I have made a struct that contains a reference member, and find
sizeof(the struct), and it's the same as sizeof(any pointer). But of
course, this could vary implementation by implementation.
 
N

newbiecpp

I got more confused regarding the size of a reference. I wrote the program:

char c;
char& rc = c;

struct A {
A() : i(c) {}
char i;
int j;
};

int main()
{
std::cout << sizeof(rc) << std::endl << sizeof(A) << std::endl;
return 0;
}

The sizeof(rc) is 1 and sizeof(A) is 8. The first looks like the reference
doesn't have a size but the second looks like the reference just like a
point and has a size. Can someone explain this to me? I appreciate!
 
N

newbiecpp

newbiecpp said:
I got more confused regarding the size of a reference. I wrote the program:

char c;
char& rc = c;

struct A {
A() : i(c) {}
char i;
int j;
};

int main()
{
std::cout << sizeof(rc) << std::endl << sizeof(A) << std::endl;
return 0;
}

The sizeof(rc) is 1 and sizeof(A) is 8. The first looks like the reference
doesn't have a size but the second looks like the reference just like a
point and has a size. Can someone explain this to me? I appreciate!

I made a typo. The struct is:

struct A {
A() : i(c) {}
char& i;
int j;
};
 
K

Karl Heinz Buchegger

newbiecpp said:
I got more confused regarding the size of a reference. I wrote the program:

See my reply in alt.comp.lang.learn c-c++

PS: If you decide that you want to post your question to more
then one newsgroup, then for heavens sake, don't post 2 messages
but do a crossposting. Simply add the name of the second newsgroup
to the list of groups to post to and that's it. In this way
the regulars don't answer the very same question multiple times.

Thank you.

PS2: Oh and please, don't top post. Put your reply underneath the
text you are replying to as I did it in this post. And trim the
reply to what is needed by removing everything else. It makes
life so much easier for everybody of us.

Thank you.
 
N

newbiecpp

Karl Heinz Buchegger said:
program:

See my reply in alt.comp.lang.learn c-c++

PS: If you decide that you want to post your question to more
then one newsgroup, then for heavens sake, don't post 2 messages
but do a crossposting. Simply add the name of the second newsgroup
to the list of groups to post to and that's it. In this way
the regulars don't answer the very same question multiple times.

Thank you.

PS2: Oh and please, don't top post. Put your reply underneath the
text you are replying to as I did it in this post. And trim the
reply to what is needed by removing everything else. It makes
life so much easier for everybody of us.

Thank you.

Excuse me for my ignorance. I am new to the news group. I will follow your
suggestion. Thanks again!
 
J

josh

Siemel said:
But in the common case of a function receiving arguments by reference, even
optimizing compilers can't optimize away the reference itself (except in
rare theoretical cases).

If the compiler expands the function inline it should usually be possible.

But the examples you often see for references use a reference to a local
variable for simplicity, and those can always be optimized away.
(unless it's volatile... are volatile references standard?)

-josh
 
V

Victor Bazarov

josh said:
If the compiler expands the function inline it should usually be possible.

But the examples you often see for references use a reference to a local
variable for simplicity, and those can always be optimized away. (unless
it's volatile... are volatile references standard?)

Since references are not objects, cv-qualifiers do not apply to them.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top