Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > structure address = structure first field address ?

Reply
Thread Tools

structure address = structure first field address ?

 
 
Vincent De Groote
Guest
Posts: n/a
 
      04-29-2009
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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      04-29-2009
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
jameskuyper
Guest
Posts: n/a
 
      04-29-2009
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)
 
Reply With Quote
 
kid joe
Guest
Posts: n/a
 
      04-29-2009
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! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-30-2009
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.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-30-2009
kid joe <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
kid joe
Guest
Posts: n/a
 
      05-01-2009
On Wed, 29 Apr 2009 18:16:12 -0700, Keith Thompson wrote:
> kid joe <(E-Mail Removed)> 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! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|


 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      05-01-2009
In article <gteqma$eev$(E-Mail Removed)>, kid joe <(E-Mail Removed)> 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.

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      05-01-2009
In article <gteqma$eev$(E-Mail Removed)>, kid joe <(E-Mail Removed)> 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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      05-02-2009
On Fri, 1 May 2009 13:26:43 +0000 (UTC), (E-Mail Removed)
(Kenny McCormack) wrote:

>In article <gteqma$eev$(E-Mail Removed)>, kid joe <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterate through a list of structure arrays of structure to get outthe field stuie_norris@yahoo.com.au C Programming 2 12-12-2012 09:18 PM
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
Parent first or child first? (a class structure question) Tony Young C++ 3 03-26-2006 12:59 AM
20D's first field trip. First Snake of Spring Celtic Boar Digital Photography 3 04-10-2005 09:40 PM



Advertisments