Question



 DEVELOP > c-Plus-Plus > Question

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "ThazKool"
Date: 18 Oct 2005 06:35:04 PM
Object: Question
Why is it that calling sizeof on a class that contains only one char&
returns the number 4? Should it have a 0 size?
.

User: "Jim Langston"

Title: Re: Question 19 Oct 2005 04:16:22 AM
"ThazKool" <Che.Gonzalez@gmail.com> wrote in message
news:1129678504.768466.92580@g43g2000cwa.googlegroups.com...

Why is it that calling sizeof on a class that contains only one char&
returns the number 4? Should it have a 0 size?

First, any class must have size > 0 (read that in these newsgroups. You can
search for that. So no class can have size 0.
Second, char& is reference to a character. A reference is at least a
pointer (it points to the memory) although treated differently (this is as I
understand it). So it makes sense that a reference would have the same size
at least of pointer. And my experience is in M$ VC++ .new 2003 that
pointers are size 4.
.
User: "Greg Comeau"

Title: Re: Question 19 Oct 2005 08:53:49 AM
In article <H9o5f.19046$1X5.1925@fe05.lga>,
Jim Langston <tazmaster@rocketmail.com> wrote:

"ThazKool" <Che.Gonzalez@gmail.com> wrote in message
news:1129678504.768466.92580@g43g2000cwa.googlegroups.com...

Why is it that calling sizeof on a class that contains only one char&
returns the number 4? Should it have a 0 size?


First, any class must have size > 0 (read that in these newsgroups. You can
search for that. So no class can have size 0.

Second, char& is reference to a character. A reference is at least a
pointer (it points to the memory) although treated differently (this is as I
understand it). So it makes sense that a reference would have the same size
at least of pointer. And my experience is in M$ VC++ .new 2003 that
pointers are size 4.

Whether or not references are implemmented behind the scenes
as a pointer, it's nevertheless so that sizeof(char&) is 1
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.

User: "ThazKool"

Title: Re: Question 19 Oct 2005 10:15:59 PM
When I look at the address of the char&, that is inside a class, it is
the same as the char outside the class. Hence it is a reference. Thus
the class has a member that is a ref where storage was declared outside
the class. Yet the sizeof the class (not instantiated with the new
operator) is 4. Some sample code is below. I guess my goal is to make
a class or struct that wraps a pre-existing char without creating
additional storage.
Thanks,
ThazKool
template< typename T >
class String
{
public:
explicit String<T>::String( const T* pointer ) : Data( (T&)(*pointer)
) { }
private:
T& Data;
};
.


User: "jalkadir"

Title: Re: Question 18 Oct 2005 06:42:00 PM
Code my friend, add some code.
A picture is worth.... you know!
.

User: "Jay Nabonne"

Title: Re: Question 18 Oct 2005 06:55:05 PM
On Tue, 18 Oct 2005 16:35:04 -0700, ThazKool wrote:

Why is it that calling sizeof on a class that contains only one char&
returns the number 4? Should it have a 0 size?

Why would a "char&" have 0 size?
.

User: "John Ratliff"

Title: Re: Question 18 Oct 2005 08:56:35 PM
ThazKool wrote:

Why is it that calling sizeof on a class that contains only one char&
returns the number 4? Should it have a 0 size?

I would think 1 or 4 could be valid responses, but 0...
A char& is most likely the same size as a (char*) pointer, which on your
platform would seem to be 4.
--John Ratliff
.

User: "Greg"

Title: Re: Question 19 Oct 2005 01:42:02 AM
ThazKool wrote:

Why is it that calling sizeof on a class that contains only one char&
returns the number 4? Should it have a 0 size?

No, in fact there's no expression that you could pass to sizeof() that
would return 0. Even a zero-sized one.
And although sizeof(char&) is certain to equal 1, the storage
requirements for a char& are almost certainly larger. The storage
requirements for a char& can be determined by applying the sizeof
operator to a struct that has a char& as its only member. Just as you
have done.
Greg
.
User: "Greg Comeau"

Title: Re: Question 19 Oct 2005 08:50:35 AM
In article <1129704122.921405.96740@z14g2000cwz.googlegroups.com>,
Greg <greghe@pacbell.net> wrote:

ThazKool wrote:

Why is it that calling sizeof on a class that contains only one char&
returns the number 4? Should it have a 0 size?


No, in fact there's no expression that you could pass to sizeof() that
would return 0. Even a zero-sized one.

And although sizeof(char&) is certain to equal 1

Ok.

, the storage
requirements for a char& are almost certainly larger. The storage
requirements for a char& can be determined by applying the sizeof
operator to a struct that has a char& as its only member. Just as you
have done.

The problem is that that does not always reveal said requirements.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.


User: "Default User"

Title: Re: Question 18 Oct 2005 06:42:37 PM
ThazKool wrote:

Why is it that calling sizeof on a class that contains only one char&
returns the number 4? Should it have a 0 size?

Why would it have 0? Perhaps you meant to type "1".
The reason is, padding. The implementation is allowed to add bytes as
it sees fit (usually for alignment).
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
.
User: "ThazKool"

Title: Re: Question 18 Oct 2005 06:48:51 PM
That makes sense. Maybe a compiler default. I know that Intel does
not require word alignment, but PowerPC does. Maybe a gcc 3.23
default. I guess I will have to write less code then ; }
.
User: "=?iso-8859-1?Q?Ali_=C7ehreli?="

Title: Re: Question 18 Oct 2005 07:38:30 PM
"ThazKool" <Che.Gonzalez@gmail.com> wrote in message
news:1129679331.553343.321590@g14g2000cwa.googlegroups.com...

That makes sense.

What makes sense? Are you using Google to post here? If so, please learn to
quote relevant portions of the e-mail that you are replying to.
I quote:
"Default User" <defaultuserbr@yahoo.com> wrote in message
news:3rlfjdFk1e2jU1@individual.net...

ThazKool wrote:

Why is it that calling sizeof on a class that contains only one char&
returns the number 4? Should it have a 0 size?


Why would it have 0? Perhaps you meant to type "1".

The reason is, padding. The implementation is allowed to add bytes as
it sees fit (usually for alignment).

With that, it does not make sense at all. You asked about a class containing
a 'char&', but received a response about a class containing a char. The
answer, which included padding may or may not have anything to do with the
fact.
Ali
.
User: "Default User"

Title: Re: Question 19 Oct 2005 10:54:49 AM
Ali Çehreli wrote:

"ThazKool" <Che.Gonzalez@gmail.com> wrote in message
news:1129679331.553343.321590@g14g2000cwa.googlegroups.com...

That makes sense.


What makes sense? Are you using Google to post here? If so, please
learn to quote relevant portions of the e-mail that you are replying
to.

I quote:

"Default User" <defaultuserbr@yahoo.com> wrote in message
news:3rlfjdFk1e2jU1@individual.net...

ThazKool wrote:

Why is it that calling sizeof on a class that contains only one
char& returns the number 4? Should it have a 0 size?


Why would it have 0? Perhaps you meant to type "1".

The reason is, padding. The implementation is allowed to add bytes
as it sees fit (usually for alignment).


With that, it does not make sense at all. You asked about a class
containing a 'char&', but received a response about a class
containing a char. The answer, which included padding may or may not
have anything to do with the fact.

No, I answered the question as asked. According to the Standard sizeof
a reference to a type is the same as that of the type, so sizeof char&
== sizeof char == 1.
5.3.3 Sizeof
1 The sizeof operator yields the number of bytes in the object
representation of its operand.
[snip]
2 When applied to a reference or a reference type, the result is the
size of the referenced type. When applied to a class, the result is the
number of bytes in an object of that class including any padding
required forplacing objects of that type in an array. The size of a
most derived class shall be greater than zero (1.8).
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
.
User: "=?iso-8859-1?Q?Ali_=C7ehreli?="

Title: Re: Question 19 Oct 2005 11:08:25 AM
"Default User" <defaultuserbr@yahoo.com> wrote in message
news:3rn8i8Fkh89oU1@individual.net...

Ali Çehreli wrote:

[...]

"Default User" <defaultuserbr@yahoo.com> wrote in message
news:3rlfjdFk1e2jU1@individual.net...

ThazKool wrote:

Why is it that calling sizeof on a class that contains only one
char& returns the number 4? Should it have a 0 size?


Why would it have 0? Perhaps you meant to type "1".

The reason is, padding. The implementation is allowed to add bytes
as it sees fit (usually for alignment).


With that, it does not make sense at all. You asked about a class
containing a 'char&', but received a response about a class
containing a char. The answer, which included padding may or may not
have anything to do with the fact.


No, I answered the question as asked. According to the Standard sizeof
a reference to a type is the same as that of the type, so sizeof char&
== sizeof char == 1.

The OP is not asking about the difference between sizeof(char) and
sizeof(char&). There is a class in question that contains a char&.

5.3.3 Sizeof
1 The sizeof operator yields the number of bytes in the object
representation of its operand.

[snip]

2 When applied to a reference or a reference type, the result is the
size of the referenced type.

Which is not the case here.

When applied to a class, the result is the
number of bytes in an object of that class including any padding
required forplacing objects of that type in an array. The size of a
most derived class shall be greater than zero (1.8).

The reason I said padding may or may not have anything to do with the fact,
is because we don't know how many bytes are used for char& types. If char&
is implemented as 4 bytes, there is no padding; if it's implemented as 3
bytes (very unlikely), there is 1 byte padding, etc.
Since references are mostly implemented as pointers behind the scenes, and
since 4 happens to be a very common size for a char* on common systems, I
bet there is no padding on the OP's system at all.
Ali
.
User: "Default User"

Title: Re: Question 19 Oct 2005 11:51:06 AM
Ali Çehreli wrote:

Since references are mostly implemented as pointers behind the
scenes, and since 4 happens to be a very common size for a char* on
common systems, I bet there is no padding on the OP's system at all.

Just to follow up on this, I tried an experiment, a struct with two
char references. It came out with a sizeof == 8, so it looks as though
you are correct for some implementations (this was VC++ 6).
The Standard doesn't require it of course, but it's certainly not wrong.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
.

User: "Default User"

Title: Re: Question 19 Oct 2005 11:33:48 AM
Ali Çehreli wrote:

The OP is not asking about the difference between sizeof(char) and
sizeof(char&). There is a class in question that contains a char&.

Right.

5.3.3 Sizeof
1 The sizeof operator yields the number of bytes in the object
representation of its operand.

[snip]

2 When applied to a reference or a reference type, the result is the
size of the referenced type.


Which is not the case here.

It factors in.

When applied to a class, the result is the
number of bytes in an object of that class including any padding
required forplacing objects of that type in an array. The size of a
most derived class shall be greater than zero (1.8).


The reason I said padding may or may not have anything to do with the
fact, is because we don't know how many bytes are used for char&
types. If char& is implemented as 4 bytes, there is no padding; if
it's implemented as 3 bytes (very unlikely), there is 1 byte padding,
etc.

Yes, we do. The same as for the size of the type. That's why I quoted
the Standard. Read it again.

Since references are mostly implemented as pointers behind the
scenes, and since 4 happens to be a very common size for a char* on
common systems, I bet there is no padding on the OP's system at all.

Doesn't matter. The result of sizeof char& must be 1.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
.
User: "=?iso-8859-1?Q?Ali_=C7ehreli?="

Title: Re: Question 19 Oct 2005 12:26:18 PM
"Default User" <defaultuserbr@yahoo.com> wrote in message
news:3rnarcFk8vliU1@individual.net...

Ali Çehreli wrote:

5.3.3 Sizeof
1 The sizeof operator yields the number of bytes in the object
representation of its operand.

[snip]

2 When applied to a reference or a reference type, the result is the
size of the referenced type.


Which is not the case here.


It factors in.

When applied to a class, the result is the
number of bytes in an object of that class including any padding
required forplacing objects of that type in an array. The size of a
most derived class shall be greater than zero (1.8).


The reason I said padding may or may not have anything to do with the
fact, is because we don't know how many bytes are used for char&
types. If char& is implemented as 4 bytes, there is no padding; if
it's implemented as 3 bytes (very unlikely), there is 1 byte padding,
etc.


Yes, we do.

I guess you mean that we do know the value of sizeof(char&). There is no
question about that: as the standard says, it is the same as sizeof(char),
which is by definition 1. This fact has nothing to do with the question
though, because the OP is not taking sizeof(char&). The OP is interested in
the sizeof a class containing a char&.
What we don't know is, quoting from what I wrote above, "how many bytes are
used for char& types."
class Foo
{
// the number of bytes used for this member
// is implementation dependent
char & ref_;
/* ... */
};

The same as for the size of the type. That's why I quoted
the Standard. Read it again.

I read the quoted text from the standard again. As I said earlier, it is
talking about sizeof(SomeType&) vs. sizeof(SomeType).
Besides, on a system where 1 byte has 8 bits; if one byte could be
sufficient to refer to a character (as you seem to think); a program could
refer to up to 256 distinct characters at a give time.
Yet, it is trivial to write a program that contains more than 256 Foo
objects (as writted above) referring to distinct characters.
Here is such a trivial program:
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
class Foo
{
char & ref_;
public:
explicit Foo(char & ref)
:
ref_(ref)
{}
void tell() const
{
ostringstream output;
output << hex;
output << "I am at 0x"
<< static_cast<void const *>(this)
<< " and referring to a char at 0x"
<< static_cast<void const *>(&ref_);
cout << output.str() << '\n';
}
};
int main()
{
unsigned int const count = 257;
char chars[count];
vector<Foo*> foos;
for (unsigned int i = 0; i != count; ++i)
{
foos.push_back(new Foo(chars[i]));
foos.back()->tell();
}
cout << "There are a total of " << foos.size() << " Foos\n";
// memory leaks are ignored for brevity
}

Since references are mostly implemented as pointers behind the
scenes, and since 4 happens to be a very common size for a char* on
common systems, I bet there is no padding on the OP's system at all.


Doesn't matter. The result of sizeof char& must be 1.

Let's not repeat the value of sizeof(char&) anymore. This question is not
about that.
Ali
.
User: "Default User"

Title: Re: Question 19 Oct 2005 02:08:10 PM
Ali Çehreli wrote:

I guess you mean that we do know the value of sizeof(char&). There is
no question about that: as the standard says, it is the same as
sizeof(char), which is by definition 1. This fact has nothing to do
with the question though, because the OP is not taking sizeof(char&).
The OP is interested in the sizeof a class containing a char&.

See my separate followup message.
There's nothing in the Standard specifying that as far as I can find,
but nothing prohibiting it either. Under the as-if rule, as long as the
implementation reports the size of the member to be one, it's ok. So
yes, some implementations (the two I checked) do seem to be storing
pointers, so that a struct with say two char references will report a
size of 8.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
.









  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER