Hello,
Does the C89/c90 language asserts that the address of a structure and
the address of the first field of the structure are the same ?
Tnaks for your replies and your time
Vincent De Groote
|
|
0
|
|
|
|
Reply
|
vincent.degroote (1)
|
4/29/2009 10:15:37 PM |
|
Vincent De Groote wrote:
> Hello,
>
> Does the C89/c90 language asserts that the address of a structure and
> the address of the first field of the structure are the same ?
Yes. More precisely, it states that you can convert
a pointer to the struct to a pointer of the first element's
type and use it to access the first element, or you can
convert a pointer to the first element to the struct's
type and use it to access the struct.
--
Eric Sosman
esosman@ieee-dot-org.invalid
|
|
0
|
|
|
|
Reply
|
esosman2 (2945)
|
4/29/2009 10:32:39 PM
|
|
Vincent De Groote wrote:
> Hello,
>
> Does the C89/c90 language asserts that the address of a structure and
> the address of the first field of the structure are the same ?
The standard guarantees that "A pointer to a structure object,
suitably converted, points to its initial member (or if that member is
a bit-field, then to the unit in which it resides), and vice versa.
There may be unnamed
padding within a structure object, but not at its
beginning." (6.7.2.1p13)
|
|
0
|
|
|
|
Reply
|
jameskuyper (5157)
|
4/29/2009 10:33:09 PM
|
|
On Thu, 30 Apr 2009 00:15:37 +0200, Vincent De Groote wrote:
> Hello,
>
> Does the C89/c90 language asserts that the address of a structure and
> the address of the first field of the structure are the same ?
Hi Vincent,
I think I can answer this one... No, I dont believe it does, because there
can be "padding bytes" inserted between fields of a struct, also at the
beginning and the end.
E.g. if the first field is a char and the second field is an int, the
compiler will fill out the struct so that the char is one of the bytes
in a full dword (or qword on 64bit) - and it could be any one of the bytes
(which one may depend on endianness of the machine?? but that's just a
guess).
Cheers,
Joe
--
...................... o _______________ _,
` Good Evening! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|
|
|
0
|
|
|
|
Reply
|
spamtrap7 (71)
|
4/29/2009 10:36:00 PM
|
|
>> Does the C89/c90 language asserts that the address of a structure and
>> the address of the first field of the structure are the same ?
>I think I can answer this one... No, I dont believe it does, because there
>can be "padding bytes" inserted between fields of a struct, also at the
>beginning and the end.
No, there may not be padding at the beginning. There may be padding
between fields, or at the end.
|
|
0
|
|
|
|
Reply
|
gordon13 (229)
|
4/29/2009 11:05:08 PM
|
|
Vincent De Groote wrote:
>
> Does the C89/c90 language asserts that the address of a structure
> and the address of the first field of the structure are the same ?
Yes.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
4/30/2009 12:03:23 AM
|
|
kid joe <spamtrap@spamtrap.invalid> writes:
> On Thu, 30 Apr 2009 00:15:37 +0200, Vincent De Groote wrote:
>> Does the C89/c90 language asserts that the address of a structure and
>> the address of the first field of the structure are the same ?
>
> I think I can answer this one... No, I dont believe it does, because there
> can be "padding bytes" inserted between fields of a struct, also at the
> beginning and the end.
[...]
No, that's incorrect. The standard very specifically does *not*
permit padding before the first member of a struct.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21469)
|
4/30/2009 1:16:12 AM
|
|
On Wed, 29 Apr 2009 18:16:12 -0700, Keith Thompson wrote:
> kid joe <spamtrap@spamtrap.invalid> writes:
>> On Thu, 30 Apr 2009 00:15:37 +0200, Vincent De Groote wrote:
>>> Does the C89/c90 language asserts that the address of a structure and
>>> the address of the first field of the structure are the same ?
>>
>> I think I can answer this one... No, I dont believe it does, because there
>> can be "padding bytes" inserted between fields of a struct, also at the
>> beginning and the end.
> [...]
>
> No, that's incorrect. The standard very specifically does *not*
> permit padding before the first member of a struct.
Hi Keith,
Thanks for the correction.
I find that strange... if I was a compiler writer on an endianness where
the least-significant byte was at the top end of the word, my thought to
implement a struct with char followed by int, Id allocate a whole dword
for the char, just do native-width integer operations on the dword, then
access and set the char through an 0xFF mask. But it sounds like that
wouldnt be standards-compliant.
Cheers,
Joe
--
...................... o _______________ _,
` Good Afternoon! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|
|
|
0
|
|
|
|
Reply
|
spamtrap7 (71)
|
5/1/2009 12:41:33 PM
|
|
In article <gteqma$eev$1@aioe.org>, kid joe <spamtrap@spamtrap.invalid> wrote:
....
>Hi Keith,
>
>Thanks for the correction.
Please, sir. Could I have some more?
>I find that strange... if I was a compiler writer on an endianness where
>the least-significant byte was at the top end of the word, my thought to
>implement a struct with char followed by int, Id allocate a whole dword
>for the char, just do native-width integer operations on the dword, then
>access and set the char through an 0xFF mask. But it sounds like that
>wouldnt be standards-compliant.
The way it is usually done is to put the padding *before* the beginning
of the struct - i.e., so that the struct is aligned to the type of its
first element.
|
|
0
|
|
|
|
Reply
|
gazelle3 (1598)
|
5/1/2009 1:26:43 PM
|
|
In article <gteqma$eev$1@aioe.org>, kid joe <spamtrap@spamtrap.invalid> wrote:
>On Wed, 29 Apr 2009 18:16:12 -0700, Keith Thompson wrote:
>I find that strange... if I was a compiler writer on an endianness where
>the least-significant byte was at the top end of the word,
.... and where there was no equally efficient instruction for accessing
the high-order byte...
>my thought to
>implement a struct with char followed by int, Id allocate a whole dword
>for the char, just do native-width integer operations on the dword, then
>access and set the char through an 0xFF mask. But it sounds like that
>wouldnt be standards-compliant.
Correct. C's rules do not always permit the most efficient layout of
data. But there are other considerations in standardisation, such as
existing programs that rely on being able to convert the address of
a structure to the address of its first member.
Your case is probably not a common one.
-- Richard
--
Please remember to mention me / in tapes you leave behind.
|
|
0
|
|
|
|
Reply
|
richard91 (3683)
|
5/1/2009 2:55:55 PM
|
|
kid joe wrote:
[...]
> I find that strange... if I was a compiler writer on an endianness where
> the least-significant byte was at the top end of the word,
Nit-pick: The least-significant byte is ALWAYS at the bottom of a word, by
definition; the bottom of a word is the right-most portion, when it's
represented in the usual form with least significant bits on the right.
The beginning and end of a word usually refers to the representation in
memory, also "first byte", "last byte".
|
|
0
|
|
|
|
Reply
|
blargg.ei3 (369)
|
5/1/2009 10:04:09 PM
|
|
On Fri, 1 May 2009 13:26:43 +0000 (UTC), gazelle@shell.xmission.com
(Kenny McCormack) wrote:
>In article <gteqma$eev$1@aioe.org>, kid joe <spamtrap@spamtrap.invalid> wrote:
>...
>>Hi Keith,
>>
>>Thanks for the correction.
>
>Please, sir. Could I have some more?
>
>>I find that strange... if I was a compiler writer on an endianness where
>>the least-significant byte was at the top end of the word, my thought to
>>implement a struct with char followed by int, Id allocate a whole dword
>>for the char, just do native-width integer operations on the dword, then
>>access and set the char through an 0xFF mask. But it sounds like that
>>wouldnt be standards-compliant.
>
>The way it is usually done is to put the padding *before* the beginning
>of the struct - i.e., so that the struct is aligned to the type of its
>first element.
The structure must be aligned and padding inserted as needed
everywhere except before the first member so that every member is
properly aligned.
--
Remove del for email
|
|
0
|
|
|
|
Reply
|
schwarzb3978 (1358)
|
5/2/2009 12:40:26 AM
|
|
Barry Schwarz wrote:
> gazelle@shell.xmission.com (Kenny McCormack) wrote:
>
.... snip ...
>
>> The way it is usually done is to put the padding *before* the
>> beginning of the struct - i.e., so that the struct is aligned
>> to the type of its first element.
>
> The structure must be aligned and padding inserted as needed
> everywhere except before the first member so that every member
> is properly aligned.
Bear in mind that McCormack is an admitted troll. From the C
standard:
7.20.3 Memory management functions
[#1] The order and contiguity of storage allocated by
successive calls to the calloc, malloc, and realloc
functions is unspecified. The pointer returned if the
allocation succeeds is suitably aligned so that it may be
assigned to a pointer to any type of object and then used to
access such an object or an array of such objects in the
space allocated (until the space is explicitly freed or
reallocated). Each such allocation shall yield a pointer to
an object disjoint from any other object. The pointer
returned points to the start (lowest byte address) of the
allocated space. If the space cannot be allocated, a null
pointer is returned. If the size of the space requested is
zero, the behavior is implementation-defined: either a null
pointer is returned, or the behavior is as if the size were
some nonzero value, except that the returned pointer shall
not be used to access an object. The value of a pointer
that refers to freed space is indeterminate.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
5/2/2009 1:07:35 AM
|
|
On Fri, 01 May 2009 21:07:35 -0400, CBFalconer <cbfalconer@yahoo.com>
wrote:
>Barry Schwarz wrote:
>> gazelle@shell.xmission.com (Kenny McCormack) wrote:
>>
>... snip ...
>>
>>> The way it is usually done is to put the padding *before* the
>>> beginning of the struct - i.e., so that the struct is aligned
>>> to the type of its first element.
>>
>> The structure must be aligned and padding inserted as needed
>> everywhere except before the first member so that every member
>> is properly aligned.
>
>Bear in mind that McCormack is an admitted troll. From the C
>standard:
>
> 7.20.3 Memory management functions
>
> [#1] The order and contiguity of storage allocated by
> successive calls to the calloc, malloc, and realloc
> functions is unspecified. The pointer returned if the
> allocation succeeds is suitably aligned so that it may be
> assigned to a pointer to any type of object and then used to
> access such an object or an array of such objects in the
> space allocated (until the space is explicitly freed or
> reallocated). Each such allocation shall yield a pointer to
> an object disjoint from any other object. The pointer
> returned points to the start (lowest byte address) of the
> allocated space. If the space cannot be allocated, a null
> pointer is returned. If the size of the space requested is
> zero, the behavior is implementation-defined: either a null
> pointer is returned, or the behavior is as if the size were
> some nonzero value, except that the returned pointer shall
> not be used to access an object. The value of a pointer
> that refers to freed space is indeterminate.
The original post asked about defining structures, not allocating
space.
--
Remove del for email
|
|
0
|
|
|
|
Reply
|
schwarzb3978 (1358)
|
5/2/2009 3:38:58 AM
|
|
On 2 May, 02:07, CBFalconer <cbfalco...@yahoo.com> wrote:
> Barry Schwarz wrote:
> > gaze...@shell.xmission.com (Kenny McCormack) wrote:
> >> The way it is usually done is to put the padding *before* the
> >> beginning of the struct - i.e., so that the struct is aligned
> >> to the type of its first element.
>
> > The structure must be aligned and padding inserted as needed
> > everywhere except before the first member so that every member
> > is properly aligned.
>
> Bear in mind that McCormack is an admitted troll. =A0From the C
> standard:
but what he said made sense.
<snip big chunk of boiler-plate standardese>
|
|
0
|
|
|
|
Reply
|
nick_keighley_nospam (4574)
|
5/2/2009 8:38:27 AM
|
|
someone wrote:
> On 2 May, 02:07, CBFalconer <cbfalco...@yahoo.com> wrote:
>> Barry Schwarz wrote:
>> > gaze...@shell.xmission.com (Kenny McCormack) wrote:
>
>> >> The way it is usually done is to put the padding *before* the
>> >> beginning of the struct - i.e., so that the struct is aligned
>> >> to the type of its first element.
>>
>> > The structure must be aligned and padding inserted as needed
>> > everywhere except before the first member so that every member
>> > is properly aligned.
>>
>> Bear in mind that McCormack is an admitted troll. �From the C
>> standard:
>
> but what he said made sense.
I hope you are referring to Barry's statement, not to Kenny's.
-- Ralf
|
|
0
|
|
|
|
Reply
|
rwspam (81)
|
5/2/2009 10:06:20 AM
|
|
kid joe wrote:
> On Thu, 30 Apr 2009 00:15:37 +0200, Vincent De Groote wrote:
>> Hello,
>>
>> Does the C89/c90 language asserts that the address of a structure and
>> the address of the first field of the structure are the same ?
>
> Hi Vincent,
>
> I think I can answer this one... No, I dont believe it does, because
> there can be "padding bytes" inserted between fields of a struct,
> also at the beginning and the end.
>
> E.g. if the first field is a char and the second field is an int, the
> compiler will fill out the struct so that the char is one of the bytes
> in a full dword (or qword on 64bit) - and it could be any one of the
> bytes (which one may depend on endianness of the machine?? but that's
> just a guess).
I rely on the fact that the compiler gives some kind of padding/alignment
control, either via pragmas or commandline/IDE switches. I pretty much
byte-align everything (with the switch) and do "manual alignment" when
designing structs (not as hard as it sounds). The only real "gotcha" to
watch out for is pointer fields for their width is different on 32- and
64-bit platforms (put pointers at the top of the struct and subsequent field
will align nicely). I always use width-specified integral types.
|
|
0
|
|
|
|
Reply
|
tony422 (386)
|
5/2/2009 10:46:48 AM
|
|
"Tony" <tony@my.net> writes:
[...]
> I rely on the fact that the compiler gives some kind of padding/alignment
> control, either via pragmas or commandline/IDE switches. I pretty much
> byte-align everything (with the switch) and do "manual alignment" when
> designing structs (not as hard as it sounds). The only real "gotcha" to
> watch out for is pointer fields for their width is different on 32- and
> 64-bit platforms (put pointers at the top of the struct and subsequent field
> will align nicely). I always use width-specified integral types.
Why? Is it really worth the effort to do manually what the compiler
would have done for you?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21469)
|
5/2/2009 5:04:55 PM
|
|
Barry Schwarz wrote:
> Kenny McCormack wrote:
> >kid joe wrote:
[...]
> >>I find that strange... if I was a compiler writer on an endianness where
> >>the least-significant byte was at the top end of the word, my thought to
> >>implement a struct with char followed by int, Id allocate a whole dword
> >>for the char, just do native-width integer operations on the dword, then
> >>access and set the char through an 0xFF mask. But it sounds like that
> >>wouldnt be standards-compliant.
> >
> >The way it is usually done is to put the padding *before* the beginning
> >of the struct - i.e., so that the struct is aligned to the type of its
> >first element.
Any padding of this sort is made invisible by giving you the address of
the first member. This is because such padding depends on the particular
object, for example one automatic object might need the padding, while
another doesn't, while all heap-based ones don't need it (as malloc itself
aligns things for you, and gives you an aligned pointer, again for the
same reason).
> The structure must be aligned and padding inserted as needed
> everywhere except before the first member so that every member is
> properly aligned.
In a sense, padding to align the first member is inserted at the END of
the structure, since that affects the alignment of the first member of the
NEXT structure in an array.
|
|
0
|
|
|
|
Reply
|
blargg.ei3 (369)
|
5/2/2009 6:28:00 PM
|
|
Keith Thompson wrote:
> "Tony" <tony@my.net> writes:
> [...]
> > I rely on the fact that the compiler gives some kind of padding/alignment
> > control, either via pragmas or commandline/IDE switches. I pretty much
> > byte-align everything (with the switch) and do "manual alignment" when
> > designing structs (not as hard as it sounds). The only real "gotcha" to
> > watch out for is pointer fields for their width is different on 32- and
> > 64-bit platforms (put pointers at the top of the struct and subsequent
field
> > will align nicely). I always use width-specified integral types.
>
> Why? Is it really worth the effort to do manually what the compiler
> would have done for you?
The compiler won't reorder members of a struct to reduce space wasted by
padding. If one is going to create millions of objects of some structure
type and wants to reduce padding in a portable way, he should order
members from largest to smallest, for example double, long long, pointers,
long, float, int, short, char.
|
|
0
|
|
|
|
Reply
|
blargg.ei3 (369)
|
5/2/2009 6:30:46 PM
|
|
Barry Schwarz wrote:
> On Fri, 1 May 2009 13:26:43 +0000 (UTC), gazelle@shell.xmission.com
> (Kenny McCormack) wrote:
>
>> In article <gteqma$eev$1@aioe.org>, kid joe <spamtrap@spamtrap.invalid> wrote:
>> ...
>>> Hi Keith,
>>>
>>> Thanks for the correction.
>> Please, sir. Could I have some more?
>>
>>> I find that strange... if I was a compiler writer on an endianness where
>>> the least-significant byte was at the top end of the word, my thought to
>>> implement a struct with char followed by int, Id allocate a whole dword
>>> for the char, just do native-width integer operations on the dword, then
>>> access and set the char through an 0xFF mask. But it sounds like that
>>> wouldnt be standards-compliant.
>> The way it is usually done is to put the padding *before* the beginning
>> of the struct - i.e., so that the struct is aligned to the type of its
>> first element.
>
> The structure must be aligned and padding inserted as needed
> everywhere except before the first member so that every member is
> properly aligned.
Actually Kenny McCormack is 100% right. There may be padding before the
structure. Or are you so daft as the think that the structure isn't aligned on
the correct boundary for its first element? As McCormack said very clearly that
padding isn't considered part of the structure, never the less it is there!
Same thing happens when you get memory from malloc, but there it is talked about.
|
|
0
|
|
|
|
Reply
|
gldncagrls (469)
|
5/2/2009 7:07:20 PM
|
|
Golden California Girls <gldncagrls@aol.com.mil> wrote:
>Or are you so daft as the think that the structure isn't aligned on the
>correct boundary for its first element?
Typical CLC thread: 2 seconds of life followed by a solid 96-hour death
rattle.
-Beej
|
|
0
|
|
|
|
Reply
|
beej (444)
|
5/2/2009 8:14:13 PM
|
|
Beej Jorgensen wrote:
> Golden California Girls <gldncagrls@aol.com.mil> wrote:
>> Or are you so daft as the think that the structure isn't aligned on the
>> correct boundary for its first element?
>
> Typical CLC thread: 2 seconds of life followed by a solid 96-hour death
> rattle.
Wrenching back towards topicality: The quoted statement
is only partially correct. A struct must be properly aligned
for *all* of its elements, not just for the first. This is
easily seen by considering an array s[] of structs containing
an element we'll call x: Since both s[0] and s[1] are aligned
to the struct's own needs and since both s[0].x and s[1].x are
aligned to the needs of x's type, it follows that the alignment
of the struct type is a multiple of the alignment of x's type.
Since we made no assumptions about which struct element x is,
nor about its firstness or lastness or offsetof nor anything
other than that it's some element of the struct, the same
argument holds for all elements.
Note that "multiple of" does not necessarily mean "smallest
multiple of," although it would be a fairly strange compiler
that didn't make it so.
--
Eric Sosman
esosman@ieee-dot-org.invalid
|
|
0
|
|
|
|
Reply
|
esosman2 (2945)
|
5/2/2009 8:27:03 PM
|
|
beej@beej.us wrote:
> Golden California Girls <gldncagrls@aol.com.mil> wrote:
> >Or are you so daft as the think that the structure isn't aligned on the
> >correct boundary for its first element?
>
> Typical CLC thread: 2 seconds of life followed by a solid 96-hour death
> rattle.
One could consider it not padding if it's before the starting address,
since it's not really part of the object itself (not within the range
address to address+size). (just doing my part to extend the rattle)
|
|
0
|
|
|
|
Reply
|
blargg.ei3 (369)
|
5/2/2009 8:41:41 PM
|
|
On Sat, 02 May 2009 13:30:46 -0500, blargg.ei3@gishpuppy.com (blargg)
wrote:
>Keith Thompson wrote:
>> "Tony" <tony@my.net> writes:
>> [...]
>> > I rely on the fact that the compiler gives some kind of padding/alignment
>> > control, either via pragmas or commandline/IDE switches. I pretty much
>> > byte-align everything (with the switch) and do "manual alignment" when
>> > designing structs (not as hard as it sounds). The only real "gotcha" to
>> > watch out for is pointer fields for their width is different on 32- and
>> > 64-bit platforms (put pointers at the top of the struct and subsequent
>field
>> > will align nicely). I always use width-specified integral types.
>>
>> Why? Is it really worth the effort to do manually what the compiler
>> would have done for you?
>
>The compiler won't reorder members of a struct to reduce space wasted by
>padding. If one is going to create millions of objects of some structure
>type and wants to reduce padding in a portable way, he should order
>members from largest to smallest, for example double, long long, pointers,
>long, float, int, short, char.
That has nothing to do with whether you align the members yourself or
let the compiler do it.
--
Remove del for email
|
|
0
|
|
|
|
Reply
|
schwarzb3978 (1358)
|
5/3/2009 2:29:11 AM
|
|
On Sat, 2 May 2009 01:38:27 -0700 (PDT),
nick_keighley_nospam@hotmail.com wrote:
>On 2 May, 02:07, CBFalconer <cbfalco...@yahoo.com> wrote:
>> Barry Schwarz wrote:
>> > gaze...@shell.xmission.com (Kenny McCormack) wrote:
>
>
>> >> The way it is usually done is to put the padding *before* the
>> >> beginning of the struct - i.e., so that the struct is aligned
>> >> to the type of its first element.
>>
>> > The structure must be aligned and padding inserted as needed
>> > everywhere except before the first member so that every member
>> > is properly aligned.
>>
>> Bear in mind that McCormack is an admitted troll. �From the C
>> standard:
>
>but what he said made sense.
Just enough to be plausible. Unfortunately it was incomplete and not
quite accurate.
--
Remove del for email
|
|
0
|
|
|
|
Reply
|
schwarzb3978 (1358)
|
5/3/2009 2:29:11 AM
|
|
On Sat, 02 May 2009 19:07:20 GMT, Golden California Girls
<gldncagrls@aol.com.mil> wrote:
>Barry Schwarz wrote:
>> On Fri, 1 May 2009 13:26:43 +0000 (UTC), gazelle@shell.xmission.com
>> (Kenny McCormack) wrote:
>>
>>> In article <gteqma$eev$1@aioe.org>, kid joe <spamtrap@spamtrap.invalid> wrote:
>>> ...
>>>> Hi Keith,
>>>>
>>>> Thanks for the correction.
>>> Please, sir. Could I have some more?
>>>
>>>> I find that strange... if I was a compiler writer on an endianness where
>>>> the least-significant byte was at the top end of the word, my thought to
>>>> implement a struct with char followed by int, Id allocate a whole dword
>>>> for the char, just do native-width integer operations on the dword, then
>>>> access and set the char through an 0xFF mask. But it sounds like that
>>>> wouldnt be standards-compliant.
>>> The way it is usually done is to put the padding *before* the beginning
>>> of the struct - i.e., so that the struct is aligned to the type of its
>>> first element.
>>
>> The structure must be aligned and padding inserted as needed
>> everywhere except before the first member so that every member is
>> properly aligned.
>
>Actually Kenny McCormack is 100% right. There may be padding before the
The proof that his statement is incorrect is simple. Consider a
system where int must be aligned on a multiple of 4 and long on a
multiple of 8. Now consider a struct on that system
struct s {int x; long y}z;
If z is located on a multiple of 8, then there must be 4 bytes of
padding between x and y (acutely 8k+4 for some non-negative k but I
would love to here about a system where k is not 0).
If z is located on an odd multiple of 4, there must be 0 bytes of
padding (actually 8k ...)
But the padding in z is not variable. Since either location satisfies
Kenny's statement but each requires a different amount of padding, the
statement cannot be correct.
>structure. Or are you so daft as the think that the structure isn't aligned on
>the correct boundary for its first element? As McCormack said very clearly that
The structure is actually aligned for all of its members and the issue
is usually driven by the member with the strictest requirement.
>padding isn't considered part of the structure, never the less it is there!
>Same thing happens when you get memory from malloc, but there it is talked about.
Now consider malloc in this system. It must return an address
suitable for any object. Since long must be on a multiple of 8,
malloc must return a multiple of 8. But since struct s is also an
object type, it must also be aligned on a multiple of 8 with four
bytes of padding since the address returned from malloc is suitable
for it also. Therefore again, z cannot be aligned on a multiple of 4
even though it satisfies Kenny's condition.
--
Remove del for email
|
|
0
|
|
|
|
Reply
|
schwarzb3978 (1358)
|
5/3/2009 2:29:11 AM
|
|
Barry Schwarz wrote:
> CBFalconer <cbfalconer@yahoo.com> wrote:
>> Barry Schwarz wrote:
>>> gazelle@shell.xmission.com (Kenny McCormack) wrote:
>>>
>>... snip ...
>>>
>>>> The way it is usually done is to put the padding *before* the
>>>> beginning of the struct - i.e., so that the struct is aligned
>>>> to the type of its first element.
>>>
>>> The structure must be aligned and padding inserted as needed
>>> everywhere except before the first member so that every member
>>> is properly aligned.
>>
>> Bear in mind that McCormack is an admitted troll. From the C
>> standard:
>>
>> 7.20.3 Memory management functions
>>
>> [#1] The order and contiguity of storage allocated by
>> successive calls to the calloc, malloc, and realloc
>> functions is unspecified. The pointer returned if the
>> allocation succeeds is suitably aligned so that it may be
>> assigned to a pointer to any type of object and then used to
>> access such an object or an array of such objects in the
>> space allocated (until the space is explicitly freed or
>> reallocated). Each such allocation shall yield a pointer to
>> an object disjoint from any other object. The pointer
>> returned points to the start (lowest byte address) of the
>> allocated space. If the space cannot be allocated, a null
>> pointer is returned. If the size of the space requested is
>> zero, the behavior is implementation-defined: either a null
>> pointer is returned, or the behavior is as if the size were
>> some nonzero value, except that the returned pointer shall
>> not be used to access an object. The value of a pointer
>> that refers to freed space is indeterminate.
>
> The original post asked about defining structures, not allocating
> space.
The space allocated by malloc, and that allocated locally, or
statically, must be identical. Otherwise you couldn't pass around
arrays of things. The real point is that McCormack is a troll, and
is bent on disrupting c.l.c.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
5/3/2009 3:51:40 AM
|
|
nick_keighley_nospam@hotmail.com wrote:
> CBFalconer <cbfalco...@yahoo.com> wrote:
>> Barry Schwarz wrote:
>>> gaze...@shell.xmission.com (Kenny McCormack) wrote:
>
>>>> The way it is usually done is to put the padding *before* the
>>>> beginning of the struct - i.e., so that the struct is aligned
>>>> to the type of its first element.
>>
>>> The structure must be aligned and padding inserted as needed
>>> everywhere except before the first member so that every member
>>> is properly aligned.
>>
>> Bear in mind that McCormack is an admitted troll. From the C
>> standard:
>
> but what he said made sense.
But is forbidden in the standard.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
5/3/2009 3:53:05 AM
|
|
blargg wrote:
> beej@beej.us wrote:
>> Golden California Girls <gldncagrls@aol.com.mil> wrote:
>>
>>> Or are you so daft as the think that the structure isn't aligned
>>> on the correct boundary for its first element?
>>
>> Typical CLC thread: 2 seconds of life followed by a solid 96-hour
>> death rattle.
>
> One could consider it not padding if it's before the starting
> address, since it's not really part of the object itself (not
> within the range address to address+size). (just doing my part to
> extend the rattle)
Of course there are, at present, 27 messages in this thread due to
McCormacks trolling.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
5/3/2009 4:00:13 AM
|
|
I take it you are retracting your words
"everywhere except before the first member"
I'll retract mine about 100%
We can agree padded before first member (aligned) to longest basic type.
Barry Schwarz wrote:
> On Sat, 02 May 2009 19:07:20 GMT, Golden California Girls
> <gldncagrls@aol.com.mil> wrote:
>
>> Barry Schwarz wrote:
>>> On Fri, 1 May 2009 13:26:43 +0000 (UTC), gazelle@shell.xmission.com
>>> (Kenny McCormack) wrote:
>>>
>>>> In article <gteqma$eev$1@aioe.org>, kid joe <spamtrap@spamtrap.invalid> wrote:
>>>> ...
>>>>> Hi Keith,
>>>>>
>>>>> Thanks for the correction.
>>>> Please, sir. Could I have some more?
>>>>
>>>>> I find that strange... if I was a compiler writer on an endianness where
>>>>> the least-significant byte was at the top end of the word, my thought to
>>>>> implement a struct with char followed by int, Id allocate a whole dword
>>>>> for the char, just do native-width integer operations on the dword, then
>>>>> access and set the char through an 0xFF mask. But it sounds like that
>>>>> wouldnt be standards-compliant.
>>>> The way it is usually done is to put the padding *before* the beginning
>>>> of the struct - i.e., so that the struct is aligned to the type of its
>>>> first element.
>>> The structure must be aligned and padding inserted as needed
>>> everywhere except before the first member so that every member is
>>> properly aligned.
>> Actually Kenny McCormack is 100% right. There may be padding before the
>
> The proof that his statement is incorrect is simple. Consider a
> system where int must be aligned on a multiple of 4 and long on a
> multiple of 8. Now consider a struct on that system
> struct s {int x; long y}z;
>
> If z is located on a multiple of 8, then there must be 4 bytes of
> padding between x and y (acutely 8k+4 for some non-negative k but I
> would love to here about a system where k is not 0).
>
> If z is located on an odd multiple of 4, there must be 0 bytes of
> padding (actually 8k ...)
>
> But the padding in z is not variable. Since either location satisfies
> Kenny's statement but each requires a different amount of padding, the
> statement cannot be correct.
>
>> structure. Or are you so daft as the think that the structure isn't aligned on
>> the correct boundary for its first element? As McCormack said very clearly that
>
> The structure is actually aligned for all of its members and the issue
> is usually driven by the member with the strictest requirement.
>
>> padding isn't considered part of the structure, never the less it is there!
>> Same thing happens when you get memory from malloc, but there it is talked about.
>
> Now consider malloc in this system. It must return an address
> suitable for any object. Since long must be on a multiple of 8,
> malloc must return a multiple of 8. But since struct s is also an
> object type, it must also be aligned on a multiple of 8 with four
> bytes of padding since the address returned from malloc is suitable
> for it also. Therefore again, z cannot be aligned on a multiple of 4
> even though it satisfies Kenny's condition.
>
|
|
0
|
|
|
|
Reply
|
gldncagrls (469)
|
5/3/2009 7:51:57 AM
|
|
On Sun, 03 May 2009 07:51:57 GMT, Golden California Girls
<gldncagrls@aol.com.mil> wrote:
>I take it you are retracting your words
>"everywhere except before the first member"
Please don't top post.
On the contrary, there is never any padding before the first member of
a struct.
>
>I'll retract mine about 100%
>
>We can agree padded before first member (aligned) to longest basic type.
No we cannot. Even if you change "padded before first member" to
"padded before struct", it is still incorrect. We might agree if you
change "longest" to "strictest"
>
>Barry Schwarz wrote:
>> On Sat, 02 May 2009 19:07:20 GMT, Golden California Girls
>> <gldncagrls@aol.com.mil> wrote:
>>
>>> Barry Schwarz wrote:
>>>> On Fri, 1 May 2009 13:26:43 +0000 (UTC), gazelle@shell.xmission.com
>>>> (Kenny McCormack) wrote:
>>>>
>>>>> In article <gteqma$eev$1@aioe.org>, kid joe <spamtrap@spamtrap.invalid> wrote:
>>>>> ...
>>>>>> Hi Keith,
>>>>>>
>>>>>> Thanks for the correction.
>>>>> Please, sir. Could I have some more?
>>>>>
>>>>>> I find that strange... if I was a compiler writer on an endianness where
>>>>>> the least-significant byte was at the top end of the word, my thought to
>>>>>> implement a struct with char followed by int, Id allocate a whole dword
>>>>>> for the char, just do native-width integer operations on the dword, then
>>>>>> access and set the char through an 0xFF mask. But it sounds like that
>>>>>> wouldnt be standards-compliant.
>>>>> The way it is usually done is to put the padding *before* the beginning
>>>>> of the struct - i.e., so that the struct is aligned to the type of its
>>>>> first element.
>>>> The structure must be aligned and padding inserted as needed
>>>> everywhere except before the first member so that every member is
>>>> properly aligned.
>>> Actually Kenny McCormack is 100% right. There may be padding before the
>>
>> The proof that his statement is incorrect is simple. Consider a
>> system where int must be aligned on a multiple of 4 and long on a
>> multiple of 8. Now consider a struct on that system
>> struct s {int x; long y}z;
>>
>> If z is located on a multiple of 8, then there must be 4 bytes of
>> padding between x and y (acutely 8k+4 for some non-negative k but I
>> would love to here about a system where k is not 0).
>>
>> If z is located on an odd multiple of 4, there must be 0 bytes of
>> padding (actually 8k ...)
>>
>> But the padding in z is not variable. Since either location satisfies
>> Kenny's statement but each requires a different amount of padding, the
>> statement cannot be correct.
>>
>>> structure. Or are you so daft as the think that the structure isn't aligned on
>>> the correct boundary for its first element? As McCormack said very clearly that
>>
>> The structure is actually aligned for all of its members and the issue
>> is usually driven by the member with the strictest requirement.
>>
>>> padding isn't considered part of the structure, never the less it is there!
>>> Same thing happens when you get memory from malloc, but there it is talked about.
>>
>> Now consider malloc in this system. It must return an address
>> suitable for any object. Since long must be on a multiple of 8,
>> malloc must return a multiple of 8. But since struct s is also an
>> object type, it must also be aligned on a multiple of 8 with four
>> bytes of padding since the address returned from malloc is suitable
>> for it also. Therefore again, z cannot be aligned on a multiple of 4
>> even though it satisfies Kenny's condition.
>>
--
Remove del for email
|
|
0
|
|
|
|
Reply
|
schwarzb3978 (1358)
|
5/3/2009 6:03:18 PM
|
|
Barry Schwarz wrote:
> On Sun, 03 May 2009 07:51:57 GMT, Golden California Girls
> <gldncagrls@aol.com.mil> wrote:
>
>> I take it you are retracting your words
>> "everywhere except before the first member"
>
> Please don't top post.
When I don't want to pad, I will.
>
> On the contrary, there is never any padding before the first member of
> a struct.
So now you claim a struct is not aligned. Interesting.
You are obviously having a problem with the concept of "before" and maybe are
limiting it in some unspecified manner.
Hint, see if the standard defines the word padding. It uses it in many places
to describe filler things and even describes as padding the filler used to get
alignment.
|
|
0
|
|
|
|
Reply
|
gldncagrls (469)
|
5/3/2009 11:38:49 PM
|
|
In article <dWpLl.2220$fy.1897@nwrddc01.gnilink.net>,
Golden California Girls <gldncagrls@aol.com.mil> wrote:
>> On the contrary, there is never any padding before the first member of
>> a struct.
>So now you claim a struct is not aligned. Interesting.
>
>You are obviously having a problem with the concept of "before" and maybe are
>limiting it in some unspecified manner.
>
>Hint, see if the standard defines the word padding. It uses it in many places
>to describe filler things and even describes as padding the filler used to get
>alignment.
As far as I can see, it is used only to describe unused bits or bytes
within objects. An implementation may leave memory unused in order to
ensure alignment of a single struct, but that unused memory is not
part of any C object.
In any case, it's obvious that everyone who has said there is no
padding before the first member of a struct means, correctly, that
there is no padding *within the struct* before the first member.
There is no real dispute here, just an attempt to make people seem
wrong when they are in fact right.
-- Richard
--
Please remember to mention me / in tapes you leave behind.
|
|
0
|
|
|
|
Reply
|
richard91 (3683)
|
5/4/2009 12:06:22 AM
|
|
On Sun, 03 May 2009 23:38:49 GMT, Golden California Girls
<gldncagrls@aol.com.mil> wrote:
>Barry Schwarz wrote:
>> On Sun, 03 May 2009 07:51:57 GMT, Golden California Girls
>> <gldncagrls@aol.com.mil> wrote:
>>
>>> I take it you are retracting your words
>>> "everywhere except before the first member"
>>
>> Please don't top post.
>
>When I don't want to pad, I will.
>
>>
>> On the contrary, there is never any padding before the first member of
>> a struct.
>
>So now you claim a struct is not aligned. Interesting.
As long as you want to stick stupid words in my mouth, since there is
usually not any padding in an int, I guess I'm also saying that an int
is not aligned either.
>
>You are obviously having a problem with the concept of "before" and maybe are
>limiting it in some unspecified manner.
>
>
>Hint, see if the standard defines the word padding. It uses it in many places
>to describe filler things and even describes as padding the filler used to get
>alignment.
You mean like section 6.5.3.4-3 which says a structure may have
internal and trailing padding but specifically omits leading padding.
Or perhaps you mean 6.7.2.1-13 which says there may be unnamed padding
within a structure but not at its beginning.
--
Remove del for email
|
|
0
|
|
|
|
Reply
|
schwarzb3978 (1358)
|
5/4/2009 6:59:49 AM
|
|
Barry Schwarz wrote:
> You mean like section 6.5.3.4-3 which says a structure may have
> internal and trailing padding but specifically omits leading padding.
> Or perhaps you mean 6.7.2.1-13 which says there may be unnamed padding
> within a structure but not at its beginning.
May be. They seem to limit before to only the case of within, something I don't
believe the OP stated.
|
|
0
|
|
|
|
Reply
|
gldncagrls (469)
|
5/5/2009 12:10:25 AM
|
|
Golden California Girls <gldncagrls@aol.com.mil> wrote:
>May be. They seem to limit before to only the case of within,
>something I don't believe the OP stated.
The OP merely said:
OP> Does the C89/c90 language asserts that the address of a structure
OP> and the address of the first field of the structure are the same?
It's apparent that he's speaking "within the struct" and not before it.
And CBFalconer won the prize for "Most Concise Answer" right off the
line.
It's also apparent that all posters on this thread agree on where the
compiler can and cannot add padding in a struct. So why can't we all
just all get along? :)
-Beej
|
|
0
|
|
|
|
Reply
|
beej (444)
|
5/5/2009 12:33:26 AM
|
|
"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnocubigg8.fsf@nuthaus.mib.org...
> "Tony" <tony@my.net> writes:
> [...]
>> I rely on the fact that the compiler gives some kind of padding/alignment
>> control, either via pragmas or commandline/IDE switches. I pretty much
>> byte-align everything (with the switch) and do "manual alignment" when
>> designing structs (not as hard as it sounds). The only real "gotcha" to
>> watch out for is pointer fields for their width is different on 32- and
>> 64-bit platforms (put pointers at the top of the struct and subsequent
>> field
>> will align nicely). I always use width-specified integral types.
>
> Why? Is it really worth the effort to do manually what the compiler
> would have done for you?
Yes, as an aspiring compiler writer it does. Also, having complete control
of your code is important: read, the portability mantra is a white elephant
(or yet to be one). All those behind the scenes things the compiler does
get's annoying many times (moreso in C++, but there, one thing leads to
another and that makes it a big pill), and I have different goals for my
language that the C language has.
Tony
|
|
0
|
|
|
|
Reply
|
tony422 (386)
|
5/19/2009 4:07:40 AM
|
|
"blargg" <blargg.ei3@gishpuppy.com> wrote in message
news:blargg.ei3-0205091330460001@192.168.1.4...
> Keith Thompson wrote:
>> "Tony" <tony@my.net> writes:
>> [...]
>> > I rely on the fact that the compiler gives some kind of
>> > padding/alignment
>> > control, either via pragmas or commandline/IDE switches. I pretty much
>> > byte-align everything (with the switch) and do "manual alignment" when
>> > designing structs (not as hard as it sounds). The only real "gotcha" to
>> > watch out for is pointer fields for their width is different on 32- and
>> > 64-bit platforms (put pointers at the top of the struct and subsequent
> field
>> > will align nicely). I always use width-specified integral types.
>>
>> Why? Is it really worth the effort to do manually what the compiler
>> would have done for you?
>
> The compiler won't reorder members of a struct to reduce space wasted by
> padding. If one is going to create millions of objects of some structure
> type and wants to reduce padding in a portable way, he should order
> members from largest to smallest, for example double, long long, pointers,
> long, float, int, short, char.
I rather have control via a switch or pragma rather than "a portable way".
Tony
|
|
0
|
|
|
|
Reply
|
tony422 (386)
|
5/19/2009 4:09:41 AM
|
|
|
38 Replies
29 Views
(page loaded in 0.337 seconds)
Similiar Articles: CONTAINING_RECORD macro - comp.lang.c++.moderated... the first line. It looks like this macro calculates the offset of "field" within the structure "type" given the address, i.e ... of the structure and the address of a field How can I rename a structure name inside a loop? - comp.soft-sys ...if strcmp(John.id , first structure input by the ... which have three similar fields: id, address ... ypu sort on the length of the name field. Use of a comparision function ... Sorting address field - comp.databases.filemakerNow, > if I sort by "address" field, it sorts by ... middlewords(address, 2, wordcount(address)-1). Now you can sort first by ... Reconsider your data structure. If you need to ... Global Variable vs Struct Variable Question - comp.lang.asm.x86 ...... function by a pointer to the address to that function is a ... have a pointer to the correct method/function within the structure ... The first is more flexible. The real ... Passing entire handles structure from 1 gui to another - comp.soft ...Attempt to reference field of non-structure array. - comp.soft ..... to a function - comp.lang.c ... comp.lang.asm.x86 ..... call the first function, you pass the address ... array of strings - comp.soft-sys.sasYou might want to look up the scan() function. May be that's all ... pass the address of the structure which will also be the address of the first element of the structure. How do I parse a comma separated field? - comp.databases.mysql ...... 32 > > TIA You don't you build a table structure ... > > you can use a function called substring_index to ... A calc field defined as Left(address,Position( address , "," , 1 ... Preferred way to get multiple returns from a function - comp.lang ...Consolidate field contents from across a found group of records ... The ValueListItems() function will still return the first ... ... answer = function ... have a structure ... read() error - Bad Address - comp.unix.programmer... of the client and it's sockaddr_in structure are passed as arguments to the function ... This is the first time I'm using threads ... read() error in handle_client: Bad address ... Speed-up the reading of large binary files with complex structures ...... uint32'); Direct access is faster than dynamic field ... the equivalent of a type-pun using an invalid address for ... First I read all the data in the memory using ... struct/union difference - comp.lang.c++.moderatedThat's what an anonymous structure does. This wouldn't ... no requirement that i should, in effect have an address ... lang.c++.moderated ... free encyclopedia A bit field ... Difference between Call and Callback ??? - comp.lang.asm.x86 ...You psss the address to the OS in a WNDCLASSEX structure and the ... Call and Callback ??? - comp.lang.asm.x86 ..... call the first function, you pass the address of ... Consolidate field contents from across a found group of records ...Create a value list that uses all values of the email address field ... The ValueListItems() function will still return the first values, but will return all of them ... calling member functions from WNPROC callback functions - comp ...{Thopugh this, at first glance is Windows ... value should be passed to the callback function. Pass the address of ... directly into the extra space windows structure. ctypes: How to call unexported functions in a dll - comp.lang ...One solution is to first create a prototype for the function ... prototype with the address of the dll function ... To call a function that has a non-blittable structure as its ... structure address = structure first field address ?Hello, Does the C89/c90 language asserts that the address of a structure and the address of the first field of the structure are the same ? Tnaks for your replies and ... Socket Address Structures | UNIX Network Programming: Sockets ...This function copies the socket address structure from the ... field, both the length field in the socket address structure ... If you didn’t catch the first ... 7/22/2012 8:00:50 AM
|