 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rick Holloman Guest
|
Posted: Sat Dec 11, 2004 3:22 pm Post subject: size of char array vs chars |
|
|
If I had a structure that contained 128 individual consecutive char elements
are they guaranteed to lay out in memory the same as an array of 128 chars?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Sat Dec 11, 2004 11:55 pm Post subject: Re: size of char array vs chars |
|
|
Rick Holloman wrote:
| Quote: | If I had a structure that contained 128 individual consecutive char elements
are they guaranteed to lay out in memory the same as an array of 128 chars?
|
Not by the standard, but popular compilers offer switches and options to
control the layout and padding of structure elements.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Sat Dec 11, 2004 11:56 pm Post subject: Re: size of char array vs chars |
|
|
"Rick Holloman" <richard.t.holloman (AT) boeing (DOT) com> wrote
| Quote: | If I had a structure that contained 128 individual consecutive char
elements
are they guaranteed to lay out in memory the same as an array of 128
chars?
|
No.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sun Dec 12, 2004 12:02 am Post subject: Re: size of char array vs chars |
|
|
Rick Holloman wrote:
| Quote: | If I had a structure that contained 128 individual consecutive char
elements
are they guaranteed to lay out in memory the same as an array of 128
chars? |
Not by the standard. As a quality of implementation issue, however, I
would certainly hope that that would be the case in practice.
--
James Kanze home: www.gabi-soft.fr
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre S=E9mard, 78210 St.-Cyr-l'=C9cole, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bo Rydberg Guest
|
Posted: Sun Dec 12, 2004 10:02 am Post subject: Re: size of char array vs chars |
|
|
"Rick Holloman" <richard.t.holloman (AT) boeing (DOT) com> wrote
| Quote: | If I had a structure that contained 128 individual consecutive char
elements
are they guaranteed to lay out in memory the same as an array of 128
chars?
|
It could be but this is compiler setup dependent. You need to tell your
compiler the struct member alignment. Usually this is 2, 4, or 8-byte
depending on the environment you are working in. But it should also let you
have a 1-byte alignment.
--
mvh // Bo Rydberg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Sun Dec 12, 2004 10:04 am Post subject: Re: size of char array vs chars |
|
|
On 11 Dec 2004 10:22:28 -0500, "Rick Holloman"
<richard.t.holloman (AT) boeing (DOT) com> wrote:
| Quote: | If I had a structure that contained 128 individual consecutive char elements
are they guaranteed to lay out in memory the same as an array of 128 chars?
|
The standard does make this guarantee for POD-struct types (see
section 9.2, paragraph 14 of the C++ standard). It is unclear to me
whether or not the same holds true for non-static members of a non-POD
type ... the standard only says that "later members have higher
addresses" (see paragraph 12 of the same section).
But since sizeof(char) is guaranteed to be 1 by the standard, I cannot
see how any implementation-defined packing or alignment would cause
them not to be allocated contiguously.
I would recommend using a std::vector<char> instead of an array
because the standard does guarantee that std::vector must allocate
storage in contiguous memory blocks (see section 23.2.4 of the
standard).
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Sun Dec 12, 2004 10:10 am Post subject: Re: size of char array vs chars |
|
|
On 11 Dec 2004 10:22:28 -0500, "Rick Holloman"
<richard.t.holloman (AT) boeing (DOT) com> wrote in comp.lang.c++.moderated:
| Quote: | If I had a structure that contained 128 individual consecutive char elements
are they guaranteed to lay out in memory the same as an array of 128 chars?
|
No, there is not. I do not think there is any implementation which
would not do it this way, but there is no such requirement or
guarantee in the C++ language standard.
Even in a POD structure with no access specifiers, the implementation
is allowed to insert padding bytes at the end of the structure and
after any member.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sun Dec 12, 2004 11:03 pm Post subject: Re: size of char array vs chars |
|
|
In article <5lsmr09fm49ver9mrltabeotnlukq20h8p (AT) 4ax (DOT) com>, Bob Hairgrove
<invalid (AT) bigfoot (DOT) com> writes
| Quote: | If I had a structure that contained 128 individual consecutive char elements
are they guaranteed to lay out in memory the same as an array of 128 chars?
The standard does make this guarantee for POD-struct types (see
section 9.2, paragraph 14 of the C++ standard).
|
I completely fail to see how that text (or anything else in the
Standard) makes the guarantee you seem to think it does. AFAIK the
compiler is always free to add padding between members. All 9.2, para 14
requires is that it do it the same way for all POD class types that
contain the same sequence of member types.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Sun Dec 12, 2004 11:06 pm Post subject: Re: size of char array vs chars |
|
|
[email]invalid (AT) bigfoot (DOT) com[/email] (Bob Hairgrove) wrote (abridged):
| Quote: | But since sizeof(char) is guaranteed to be 1 by the standard, I cannot
see how any implementation-defined packing or alignment would cause
them not to be allocated contiguously.
|
An implementation might use padding bytes for error detection, as a way of
detecting buffer overruns or other memory corruption. I believe VC++7.1
will do this for the stack, given the right commandline options. The idea
is to set the padding bytes to some known value and periodically check
whether they have been changed.
-- Dave Harris, Nottingham, UK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Sun Dec 12, 2004 11:08 pm Post subject: Re: size of char array vs chars |
|
|
Bob Hairgrove wrote:
| Quote: | The standard does make this guarantee for POD-struct types (see
section 9.2, paragraph 14 of the C++ standard).
|
No. This paragraph tells you whether *two* POD-struct types are layout
compatible, not whether one POD-struct type is compatible with any array.
| Quote: | But since sizeof(char) is guaranteed to be 1 by the standard, I cannot
see how any implementation-defined packing or alignment would cause
them not to be allocated contiguously.
|
Some implementations may prefer to align members to the word-boundary
for performance reasons.
In any case, the padding between members is unspecified, but as stated
elsewhere, popular implementations allow to control it.
| Quote: | I would recommend using a std::vector<char> instead of an array
because the standard does guarantee that std::vector must allocate
storage in contiguous memory blocks
|
And see 8.3.4/1 for the same guarantee for arrays.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Sun Dec 12, 2004 11:09 pm Post subject: Re: size of char array vs chars |
|
|
On 12 Dec 2004 05:04:01 -0500, Bob Hairgrove <invalid (AT) bigfoot (DOT) com>
wrote in comp.lang.c++.moderated:
| Quote: | On 11 Dec 2004 10:22:28 -0500, "Rick Holloman"
[email]richard.t.holloman (AT) boeing (DOT) com[/email]> wrote:
If I had a structure that contained 128 individual consecutive char elements
are they guaranteed to lay out in memory the same as an array of 128 chars?
The standard does make this guarantee for POD-struct types (see
section 9.2, paragraph 14 of the C++ standard). It is unclear to me
whether or not the same holds true for non-static members of a non-POD
type ... the standard only says that "later members have higher
addresses" (see paragraph 12 of the same section).
|
I am curious as to how you come to this interpretation of the wording
in the standard:
"Two POD struct (clause 9) types are layout compatible if they have
the same number of members, and corresponding members (in order) have
layout compatible types (3.9)."
Where do you see anything at all about layout compatibility with an
array, or any other non struct/class type?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Mon Dec 13, 2004 10:32 am Post subject: Re: size of char array vs chars |
|
|
On 12 Dec 2004 18:03:20 -0500, Francis Glassborow
<francis (AT) robinton (DOT) demon.co.uk> wrote:
| Quote: | In article <5lsmr09fm49ver9mrltabeotnlukq20h8p (AT) 4ax (DOT) com>, Bob Hairgrove
[email]invalid (AT) bigfoot (DOT) com[/email]> writes
If I had a structure that contained 128 individual consecutive char elements
are they guaranteed to lay out in memory the same as an array of 128 chars?
The standard does make this guarantee for POD-struct types (see
section 9.2, paragraph 14 of the C++ standard).
I completely fail to see how that text (or anything else in the
Standard) makes the guarantee you seem to think it does. AFAIK the
compiler is always free to add padding between members. All 9.2, para 14
requires is that it do it the same way for all POD class types that
contain the same sequence of member types.
|
OK, I think I have overlooked something. Consider the following POD
structs:
struct A {
char a1;
char a2;
// snip...
char a128;
};
struct B {
char a[128];
};
Now I see that I overlooked the fact that both A and B must have "the
same number" of non-static data members, and one array just isn't the
same as 128 single items ... IOW, exactly what the OP also wanted to
know!
It would be interesting to know what the C standard has to say about
this ... with POD types, I would assume that the C++ standard would
have to go with with the C standard here. Unfortunately, although I
have a C++ standard reference, I don't have access to the C standard.
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Rafter Guest
|
Posted: Mon Dec 13, 2004 10:37 am Post subject: Re: size of char array vs chars |
|
|
| Quote: | I would recommend using a std::vector<char> instead of an array
because the standard does guarantee that std::vector must allocate
storage in contiguous memory blocks (see section 23.2.4 of the
standard).
|
One of the reasons for this is so they are backwardly compatible
with arrays - which are also guarenteed to be allocated in
contiguous memory.
But, I too, would recommend using vector over arrays.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Dec 14, 2004 3:33 am Post subject: Re: size of char array vs chars |
|
|
In article <32kpr01uaei9n7bif5lr9pnppgs01rntp4 (AT) 4ax (DOT) com>, Bob Hairgrove
<invalid (AT) bigfoot (DOT) com> writes
| Quote: | It would be interesting to know what the C standard has to say about
this ... with POD types, I would assume that the C++ standard would
have to go with with the C standard here. Unfortunately, although I
have a C++ standard reference, I don't have access to the C standard.
|
The C Standard has exactly the same requirement. An array must be
contiguous (no padding), separate members do not have to be.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Wu Yongwei Guest
|
Posted: Thu Dec 23, 2004 3:22 pm Post subject: Re: size of char array vs chars |
|
|
Bo Rydberg wrote:
| Quote: | If I had a structure that contained 128 individual consecutive char
elements
are they guaranteed to lay out in memory the same as an array of
128
chars?
It could be but this is compiler setup dependent. You need to tell
your
compiler the struct member alignment. Usually this is 2, 4, or
8-byte
depending on the environment you are working in. But it should also
let you
have a 1-byte alignment.
|
I do not think this is correct. Struct alignment has nothing to do
with it. Try it with your compiler, and you'll see the size of the
above kind of struct won't change when the alignment changes.
Alignment affects the size of the struct in the following case:
struct foo {
int n;
char ch;
};
The size will be 5 with alignment 1, 6 with alignment 2, and 8 with
alignment 4 or 8 (assuming int has a size of 4).
Best regards,
Yongwei
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| 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
|
|