Struct alignment | | |
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 | | | | re: Struct alignment
Hipo wrote:[color=blue]
> 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?[/color]
Yes, that's mandated by the Standard.
[color=blue]
> Or would it be possible that they look like this in
> memory: a, c, d, b?[/color]
No.
[color=blue]
> I'm using the Visual Studio 2005 Pro compiler.[/color]
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 | | | | re: Struct alignment
Victor Bazarov schrieb:[color=blue]
> Hipo wrote:[color=green]
>> 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?[/color]
>
> Yes, that's mandated by the Standard.[/color]
Great, that saves my day :-)
[color=blue][color=green]
>> I'm using the Visual Studio 2005 Pro compiler.[/color]
>
> Shouldn't matter.[/color]
Hope so, standard is not always standard (bad translation of a german
saying) :-)
g, Hipo | | | | re: Struct alignment
Hipo wrote:
[color=blue][color=green][color=darkred]
>>> Can I assume that the four variables from data are stored in memory in
>>> order given above?[/color]
>>
>> Yes, that's mandated by the Standard.[/color]
>
> Great, that saves my day :-)[/color]
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 | | | | re: Struct alignment
Hipo posted:
[color=blue]
> 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[/color]
Long discussion on comp.std.c++ about this right now. Thread Title: "Struct
members -> Array elements".
-Tomás | | | | re: Struct alignment
On Thu, 25 May 2006 22:35:30 GMT, "Tomás" <No.Email@Address> wrote in
comp.lang.c++:
[color=blue]
> Hipo posted:
>[color=green]
> > 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[/color]
>
>
> Long discussion on comp.std.c++ about this right now. Thread Title: "Struct
> members -> Array elements".
>
>
> -Tomás[/color]
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 | | | | re: Struct alignment
Jack Klein posted:
[color=blue]
> No, this question is only about the order, not about the possibility
> of padding between the members.[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|