 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Bobo Guest
|
Posted: Mon Jul 14, 2003 9:57 pm Post subject: empty POD struct? |
|
|
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 ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Jul 15, 2003 3:03 am Post subject: Re: empty POD struct? |
|
|
"Bobo" <pinzo (AT) correo (DOT) nu> wrote...
| Quote: | 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 ]
|
|
| Back to top |
|
 |
Bobo Guest
|
Posted: Tue Jul 15, 2003 11:11 pm Post subject: Re: empty POD struct? |
|
|
[email]v.Abazarov (AT) attAbi (DOT) com[/email] ("Victor Bazarov") wrote in message news:<vh6benh93jp6cc (AT) corp (DOT) supernews.com>...
[snip]
| Quote: | 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 ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Jul 16, 2003 1:25 am Post subject: Re: empty POD struct? |
|
|
"Bobo" <pinzo (AT) correo (DOT) nu> wrote...
| Quote: | [...]
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 ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Jul 16, 2003 2:04 am Post subject: Re: empty POD struct? |
|
|
"Bobo" <pinzo (AT) correo (DOT) nu> wrote
| Quote: | 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++).
| Quote: | I know C++ doesn't allow zero sized objects, so sizeof(maybePOD) shall
be 1.
|
Perhaps, might be 4 or 8 for that matter.
| Quote: |
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 ]
|
|
| Back to top |
|
 |
MJ Guest
|
Posted: Fri Jul 18, 2003 8:29 pm Post subject: Re: empty POD struct? |
|
|
[email]ron (AT) sensor (DOT) com[/email] ("Ron Natalie") wrote in message news:<3f1403c2$0$24573$9a6e19ea (AT) news (DOT) newshosting.com>...
| Quote: | "Bobo" <pinzo (AT) correo (DOT) nu> wrote
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 ]
|
|
| Back to top |
|
 |
Andy Sawyer Guest
|
Posted: Sat Jul 19, 2003 9:51 pm Post subject: Re: empty POD struct? |
|
|
In article <423c153c.0307180005.1304d0a3 (AT) posting (DOT) google.com>,
on Fri, 18 Jul 2003 20:29:47 +0000 (UTC),
[email]c141592653589 (AT) hotmail (DOT) com[/email] (MJ) wrote:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Mon Jul 21, 2003 6:31 pm Post subject: Re: empty POD struct? |
|
|
[email]v.Abazarov (AT) attAbi (DOT) com[/email] ("Victor Bazarov") writes:
| Quote: | "Bobo" <pinzo (AT) correo (DOT) 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 ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Jul 22, 2003 10:56 pm Post subject: Re: empty POD struct? |
|
|
"llewelly" <llewelly.at (AT) xmission (DOT) dot.com> wrote...
| Quote: | v.Abazarov (AT) attAbi (DOT) com ("Victor Bazarov") writes:
"Bobo" <pinzo (AT) correo (DOT) 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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|