Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Struct alignment

Reply
Thread Tools

Struct alignment

 
 
Hipo
Guest
Posts: n/a
 
      05-25-2006
Hi.
Assuming a struct:

struct data{
int, a, b, c, d;
};

Can I assume that the four variables from data are stored in memory in
order given above? Or would it be possible that they look like this in
memory: a, c, d, b?

I'm using the Visual Studio 2005 Pro compiler.

g, Hipo
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-25-2006
Hipo wrote:
> Assuming a struct:
>
> struct data{
> int, a, b, c, d;
> };
>
> Can I assume that the four variables from data are stored in memory in
> order given above?


Yes, that's mandated by the Standard.

> Or would it be possible that they look like this in
> memory: a, c, d, b?


No.

> I'm using the Visual Studio 2005 Pro compiler.


Shouldn't matter.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Hipo
Guest
Posts: n/a
 
      05-25-2006
Victor Bazarov schrieb:
> Hipo wrote:
>> Assuming a struct:
>>
>> struct data{
>> int, a, b, c, d;
>> };
>>
>> Can I assume that the four variables from data are stored in memory in
>> order given above?

>
> Yes, that's mandated by the Standard.


Great, that saves my day

>> I'm using the Visual Studio 2005 Pro compiler.

>
> Shouldn't matter.


Hope so, standard is not always standard (bad translation of a german
saying)

g, Hipo
 
Reply With Quote
 
phlip
Guest
Posts: n/a
 
      05-25-2006
Hipo wrote:

>>> Can I assume that the four variables from data are stored in memory in
>>> order given above?

>>
>> Yes, that's mandated by the Standard.

>
> Great, that saves my day


You can't assume they aren't padded; that's implementation specific.

(Yes, VC++ probably doesn't pad them.)

Don't rely on it. You weren't going to treat that struct as binary data,
were you?

--
Phlip

 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      05-25-2006
Hipo posted:

> Hi.
> Assuming a struct:
>
> struct data{
> int, a, b, c, d;
> };
>
> Can I assume that the four variables from data are stored in memory in
> order given above? Or would it be possible that they look like this in
> memory: a, c, d, b?
>
> I'm using the Visual Studio 2005 Pro compiler.
>
> g, Hipo



Long discussion on comp.std.c++ about this right now. Thread Title: "Struct
members -> Array elements".


-Tomás
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      05-26-2006
On Thu, 25 May 2006 22:35:30 GMT, "Tomás" <No.Email@Address> wrote in
comp.lang.c++:

> Hipo posted:
>
> > Hi.
> > Assuming a struct:
> >
> > struct data{
> > int, a, b, c, d;
> > };
> >
> > Can I assume that the four variables from data are stored in memory in
> > order given above? Or would it be possible that they look like this in
> > memory: a, c, d, b?
> >
> > I'm using the Visual Studio 2005 Pro compiler.
> >
> > g, Hipo

>
>
> Long discussion on comp.std.c++ about this right now. Thread Title: "Struct
> members -> Array elements".
>
>
> -Tomás


No, this question is only about the order, not about the possibility
of padding between the members.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      05-26-2006
Jack Klein posted:


> No, this question is only about the order, not about the possibility
> of padding between the members.


I realise that... but the order of the members is of no relevance unless
you try to do something funky like access them with pointers:

data obj;

int *p = &obj.a;

*p = 1; /* We're assuming this is A */

*++p = 2; /* We're assuming this is B */

*++p = 3; /* We're assuming this is C */

*++p = 4; /* We're assuming this is D */


If you don't try something funky like this, then there's no reason to
care about the order of the members.

(Which makes me wonder: Why does C++ even guarantee that the members be
in order one-after-another in a POD struct? We already don't have a
guarantee of the absence of padding... so there's no point in
guaranteeing that they be in order).

Okay... well... there might be ONE use:

bool IsAhead( const int * const a, const int * const b )
{
return b > a;
}

int main()
{
data obj;

IsAhead( &obj.a, &obj.b );
}


-Tomás
 
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
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Alignment on Struct and Global Variable? Bryan Parkoff C++ 0 04-02-2005 02:32 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
Re: Struct Alignment Eric G. C Programming 2 08-04-2003 09:30 PM
Re: Struct Alignment Jack Klein C Programming 0 08-04-2003 04:31 AM



Advertisments