Topic: empty POD struct?


Author: llewelly.at@xmission.dot.com (llewelly)
Date: Mon, 21 Jul 2003 18:31:08 +0000 (UTC)
Raw View
v.Abazarov@attAbi.com ("Victor Bazarov") writes:

> "Bobo" <pinzo@correo.nu> wrote...
>> I am a bit confused about if this is a POD or not:
>> struct maybePOD
>> {
>> };
>> I think it has all the requirements for POD structs.
>>
>> I know C++ doesn't allow zero sized objects, so sizeof(maybePOD) shall
>> be 1.
>> Now let's write the following:
>> struct derStruct : maybePOD
>> {
>>    int foo;
>> };
>>
>> By means of the empty base optimization the maybePOD subobject in
>> derStruct may have no memory assigned to it. But so, the following
>> code won't work.
>>
>> template <typename T> void copyPOD(T &a, const T&b)
>> {
>>    memcpy(&a, &b, sizeof(T));
>> }
>>
>> int main()
>> {
>>    maybePOD noData, *p;
>>    derStruct obj1;
>>
>>    copyPOD(obj1, non);
>> }
>>
>> The first byte of obj1.foo will be overwritten by the unused byte from
>> noData!!!
>>
>> What's wrong with this code? O am I missing something?
>
> The Standard does not allow using memcpy to copy objects into
> subobjects.  What you're allowed to do is to copy a _complete_
> object into another _complete_ object (see 3.9/2 and 1.8/2).

The Standard also says memcpy has undefined behavior for non-POD -
    such as derStruct. (9/4 says POD are aggregates, 8.5.1/1 says
    aggregates have no base classes.)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Tue, 22 Jul 2003 22:56:05 +0000 (UTC)
Raw View
"llewelly" <llewelly.at@xmission.dot.com> wrote...
> v.Abazarov@attAbi.com ("Victor Bazarov") writes:
>
> > "Bobo" <pinzo@correo.nu> wrote...
> >> I am a bit confused about if this is a POD or not:
> >> struct maybePOD
> >> {
> >> };
> >> I think it has all the requirements for POD structs.
> >>
> >> I know C++ doesn't allow zero sized objects, so sizeof(maybePOD) shall
> >> be 1.
> >> Now let's write the following:
> >> struct derStruct : maybePOD
> >> {
> >>    int foo;
> >> };
> >>
> >> By means of the empty base optimization the maybePOD subobject in
> >> derStruct may have no memory assigned to it. But so, the following
> >> code won't work.
> >>
> >> template <typename T> void copyPOD(T &a, const T&b)
> >> {
> >>    memcpy(&a, &b, sizeof(T));
> >> }
> >>
> >> int main()
> >> {
> >>    maybePOD noData, *p;
> >>    derStruct obj1;
> >>
> >>    copyPOD(obj1, non);
> >> }
> >>
> >> The first byte of obj1.foo will be overwritten by the unused byte from
> >> noData!!!
> >>
> >> What's wrong with this code? O am I missing something?
> >
> > The Standard does not allow using memcpy to copy objects into
> > subobjects.  What you're allowed to do is to copy a _complete_
> > object into another _complete_ object (see 3.9/2 and 1.8/2).
>
> The Standard also says memcpy has undefined behavior for non-POD -
>     such as derStruct. (9/4 says POD are aggregates, 8.5.1/1 says
>     aggregates have no base classes.)

I don't think this is relevant.  The code attempts to copy 'maybePOD'
part of 'derStruct' (put aside the fact that the code is not working).
'maybePOD' _is_ a POD.

Victor


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: c141592653589@hotmail.com (MJ)
Date: Fri, 18 Jul 2003 20:29:47 +0000 (UTC)
Raw View
ron@sensor.com ("Ron Natalie") wrote in message news:<3f1403c2$0$24573$9a6e19ea@news.newshosting.com>...
> "Bobo" <pinzo@correo.nu> wrote in message news:745c4429.0307131616.7c141c03@posting.google.com...
> > Hello all.
> > I am a bit confused about if this is a POD or not:
> > struct maybePOD
> > {
> > };
> > I think it has all the requirements for POD structs.
>
> It does.   The only odd thing is that it is NOT a legal C struct (but that has no
> bearing on C++).
>
> > I know C++ doesn't allow zero sized objects, so sizeof(maybePOD) shall
> > be 1.
>
> Perhaps, might be 4 or 8 for that matter.

In fact my compiler produces a size of 8 for that case. For a class
with a char only, the size is 1:

  class a {  };        // size = 8
  class b { char c };  // size = 1

What may be the reason for the compiler to produce a size of 8 for an
empty class?


Michael

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: andys@despammed.com (Andy Sawyer)
Date: Sat, 19 Jul 2003 21:51:10 +0000 (UTC)
Raw View
In article <423c153c.0307180005.1304d0a3@posting.google.com>,
 on Fri, 18 Jul 2003 20:29:47 +0000 (UTC),
 c141592653589@hotmail.com (MJ) wrote:

> In fact my compiler produces a size of 8 for that case. For a class
> with a char only, the size is 1:
>
>   class a {  };        // size = 8
>   class b { char c };  // size = 1
>
> What may be the reason for the compiler to produce a size of 8 for an
> empty class?

Because it wants to? The standard certainly doesn't preclude doing so,
although if it bothered me, I'd probably complain to my compiler vendor.

Regards,
 Andy S.
--
"Light thinks it travels faster than anything but it is wrong. No matter
 how fast light travels it finds the darkness has always got there first,
 and is waiting for it."                  -- Terry Pratchett, Reaper Man

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: pinzo@correo.nu (Bobo)
Date: Mon, 14 Jul 2003 21:57:59 +0000 (UTC)
Raw View
Hello all.
I am a bit confused about if this is a POD or not:
struct maybePOD
{
};
I think it has all the requirements for POD structs.

I know C++ doesn't allow zero sized objects, so sizeof(maybePOD) shall
be 1.
Now let's write the following:
struct derStruct : maybePOD
{
   int foo;
};

By means of the empty base optimization the maybePOD subobject in
derStruct may have no memory assigned to it. But so, the following
code won't work.

template <typename T> void copyPOD(T &a, const T&b)
{
   memcpy(&a, &b, sizeof(T));
}

int main()
{
   maybePOD noData, *p;
   derStruct obj1;

   copyPOD(obj1, non);
}

The first byte of obj1.foo will be overwritten by the unused byte from
noData!!!

What's wrong with this code? O am I missing something?

TIA
Bobo.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Tue, 15 Jul 2003 03:03:04 +0000 (UTC)
Raw View
"Bobo" <pinzo@correo.nu> wrote...
> I am a bit confused about if this is a POD or not:
> struct maybePOD
> {
> };
> I think it has all the requirements for POD structs.
>
> I know C++ doesn't allow zero sized objects, so sizeof(maybePOD) shall
> be 1.
> Now let's write the following:
> struct derStruct : maybePOD
> {
>    int foo;
> };
>
> By means of the empty base optimization the maybePOD subobject in
> derStruct may have no memory assigned to it. But so, the following
> code won't work.
>
> template <typename T> void copyPOD(T &a, const T&b)
> {
>    memcpy(&a, &b, sizeof(T));
> }
>
> int main()
> {
>    maybePOD noData, *p;
>    derStruct obj1;
>
>    copyPOD(obj1, non);
> }
>
> The first byte of obj1.foo will be overwritten by the unused byte from
> noData!!!
>
> What's wrong with this code? O am I missing something?

The Standard does not allow using memcpy to copy objects into
subobjects.  What you're allowed to do is to copy a _complete_
object into another _complete_ object (see 3.9/2 and 1.8/2).

Victor


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: pinzo@correo.nu (Bobo)
Date: Tue, 15 Jul 2003 23:11:10 +0000 (UTC)
Raw View
v.Abazarov@attAbi.com ("Victor Bazarov") wrote in message news:<vh6benh93jp6cc@corp.supernews.com>...

[snip]

> The Standard does not allow using memcpy to copy objects into
> subobjects.  What you're allowed to do is to copy a _complete_
> object into another _complete_ object (see 3.9/2 and 1.8/2).

THX for the answer. I'm sorry but don't have a copy of the standard,
but following the draft, the 3.9/2 is about copying the object into an
array of chars. 3.9/3 is about copying from a T* into another T*, and
in this paragraph there is no reference to _complete_ object.

And what has 1.8/2 to do with this? It's about
_implementation_defined_.

Best regards.
Bobo.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Wed, 16 Jul 2003 01:25:00 +0000 (UTC)
Raw View
"Bobo" <pinzo@correo.nu> wrote...
> [...]
> THX for the answer. I'm sorry but don't have a copy of the standard,
> but following the draft, [...]

Get a copy of the Standard.  Otherwise you have no idea what
people here are talking about.

Victor

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@sensor.com ("Ron Natalie")
Date: Wed, 16 Jul 2003 02:04:15 +0000 (UTC)
Raw View
"Bobo" <pinzo@correo.nu> wrote in message news:745c4429.0307131616.7c141c03@posting.google.com...
> Hello all.
> I am a bit confused about if this is a POD or not:
> struct maybePOD
> {
> };
> I think it has all the requirements for POD structs.

It does.   The only odd thing is that it is NOT a legal C struct (but that has no
bearing on C++).

> I know C++ doesn't allow zero sized objects, so sizeof(maybePOD) shall
> be 1.

Perhaps, might be 4 or 8 for that matter.

>
> The first byte of obj1.foo will be overwritten by the unused byte from
> noData!!!
>
> What's wrong with this code? O am I missing something?

You shouldn't use memcpy.   The assignment operator would have done the
right thing.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: do-not-spam-benh@bwsint.com (Ben Hutchings)
Date: Wed, 16 Jul 2003 14:50:21 +0000 (UTC)
Raw View
In article <od1Ra.73139$Ph3.7285@sccrnsc04>, "Victor Bazarov" wrote:
> "Bobo" <pinzo@correo.nu> wrote...
>> [...]
>> THX for the answer. I'm sorry but don't have a copy of the standard,
>> but following the draft, [...]
>
> Get a copy of the Standard.  Otherwise you have no idea what
> people here are talking about.

The text of these paragraphs is identical in CD2 and the standard,
though paragraph 1.8/2 was numbered 1.7/2 in CD2.

It does seem like the standard is in error here.  3.9/2 says "For any
complete POD object type T, ...".  I think the word "of" is missing
before "type".  3.9/3 says "For any type T, ..." with no "complete"
qualification.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jdennett@acm.org (James Dennett)
Date: Wed, 16 Jul 2003 15:06:47 +0000 (UTC)
Raw View
Ben Hutchings wrote:
> In article <od1Ra.73139$Ph3.7285@sccrnsc04>, "Victor Bazarov" wrote:
>
>>"Bobo" <pinzo@correo.nu> wrote...
>>
>>>[...]
>>>THX for the answer. I'm sorry but don't have a copy of the standard,
>>>but following the draft, [...]
>>
>>Get a copy of the Standard.  Otherwise you have no idea what
>>people here are talking about.
>
>
> The text of these paragraphs is identical in CD2 and the standard,
> though paragraph 1.8/2 was numbered 1.7/2 in CD2.
>
> It does seem like the standard is in error here.  3.9/2 says "For any
> complete POD object type T, ...".  I think the word "of" is missing
> before "type".  3.9/3 says "For any type T, ..." with no "complete"
> qualification.

Core issue 43 corrected 3.9/3, requiring that memcpy not be used to
copy base subobjects.  The new text says
   For any POD type T, if two pointers to T point to distinct
   T objects obj1 and obj2, where neither obj1 nor obj2 is a base-class
   subobject, if the value of obj1 is copied into obj2, using
   the memcpy library function, obj2 shall subsequently hold the same
   value as obj1.

This text is in the revised standard 14882:2003.

-- James.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rmaddox@isicns.com (Randy Maddox)
Date: Wed, 16 Jul 2003 21:27:55 +0000 (UTC)
Raw View
pinzo@correo.nu (Bobo) wrote in message news:<745c4429.0307151457.5a8fc997@posting.google.com>...
> v.Abazarov@attAbi.com ("Victor Bazarov") wrote in message news:<vh6benh93jp6cc@corp.supernews.com>...
>
> [snip]
>
> > The Standard does not allow using memcpy to copy objects into
> > subobjects.  What you're allowed to do is to copy a _complete_
> > object into another _complete_ object (see 3.9/2 and 1.8/2).
>
> THX for the answer. I'm sorry but don't have a copy of the standard,
> but following the draft, the 3.9/2 is about copying the object into an
> array of chars. 3.9/3 is about copying from a T* into another T*, and
> in this paragraph there is no reference to _complete_ object.

Go to www.ansi.org and download a .pdf copy of the Standard for $18US.

Randy.

>
> And what has 1.8/2 to do with this? It's about
> _implementation_defined_.
>
> Best regards.
> Bobo.
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]