By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Size of a reference?

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
newbiecpp wrote:
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
Jul 22 '05 #2

P: n/a
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ó:
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.

Jul 22 '05 #3

P: n/a
In message <iz*****************@twister.auna.com>, Carlos Martinez
Garcia <si***************@nospam.yahoo.es> top-posted
newbiecpp escribió:
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.
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.

--
Richard Herring
Jul 22 '05 #4

P: n/a
newbiecpp wrote:
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

Jul 22 '05 #5

P: n/a
"josh" <sm*************************@yahoo.com.NOSPAM> wrote in message
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.
Jul 22 '05 #6

P: n/a
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!
"newbiecpp" <ne*******@yahoo.com> wrote in message
news:1RNRc.1911$Po1.1275@trndny08...
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.

Jul 22 '05 #7

P: n/a

"newbiecpp" <ne*******@yahoo.com> wrote in message
news:JAnSc.4354$Kv2.1065@trndny09...
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;
};

"newbiecpp" <ne*******@yahoo.com> wrote in message
news:1RNRc.1911$Po1.1275@trndny08...
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.


Jul 22 '05 #8

P: n/a
newbiecpp wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #9

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41**************@gascad.at...
newbiecpp wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at


Excuse me for my ignorance. I am new to the news group. I will follow your
suggestion. Thanks again!
Jul 22 '05 #10

P: n/a
newbiecpp wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41**************@gascad.at...
newbiecpp wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at


Excuse me for my ignorance.


No problem. We all started somewhere.
I am new to the news group. I will follow your
suggestion. Thanks again!


I thank you.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #11

P: n/a
Siemel Naran wrote:
"josh" <sm*************************@yahoo.com.NOSPAM> wrote in message
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).


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

Jul 22 '05 #12

P: n/a
josh wrote:
Siemel Naran wrote:
"josh" <sm*************************@yahoo.com.NOSPAM> wrote in message
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).

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
Jul 22 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.