Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Alignment

Reply
Thread Tools

Alignment

 
 
better_cs_now@yahoo.com
Guest
Posts: n/a
 
      02-26-2009
Hello All,

Please consider the two definitions below:

Foo foo;
unsigned char foo[sizeof(Foo)];

Are these guaranteed to have the same alignment, or will the second
definition have an alignment that is only modulo 1?

Thanks,
Dave
 
Reply With Quote
 
 
 
 
Gert-Jan de Vos
Guest
Posts: n/a
 
      02-26-2009
On Feb 26, 7:00*pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> Foo foo;
> unsigned char foo[sizeof(Foo)];
>
> Are these guaranteed to have the same alignment, or will the second
> definition have an alignment that is only modulo 1?


The standard has the answer in 3.1-5 [basic.types]:

"Object types have alignment requirements (3.9.1, 3.9.2). The
alignment of a complete object type is an implementation-defined
integer value representing a number of bytes; an object is allocated
at an address that meets the alignment requirements of its object
type."

So, unless your Foo is a typedef for an array of unsigned char, then
your foo objects are two different types. Therefore both have their
own implementation-defined alignment en you can not portably assume
they will be the same.
 
Reply With Quote
 
 
 
 
better_cs_now@yahoo.com
Guest
Posts: n/a
 
      02-26-2009
On Feb 26, 11:18*am, Gert-Jan de Vos <gert-
(E-Mail Removed)> wrote:
> On Feb 26, 7:00*pm, "(E-Mail Removed)"
>
> <(E-Mail Removed)> wrote:
> > Foo foo;
> > unsigned char foo[sizeof(Foo)];

>
> > Are these guaranteed to have the same alignment, or will the second
> > definition have an alignment that is only modulo 1?

>
> The standard has the answer in 3.1-5 [basic.types]:
>
> "Object types have alignment requirements (3.9.1, 3.9.2). The
> alignment of a complete object type is an implementation-defined
> integer value representing a number of bytes; an object is allocated
> at an address that meets the alignment requirements of its object
> type."
>
> So, unless your Foo is a typedef for an array of unsigned char, then
> your foo objects are two different types. Therefore both have their
> own implementation-defined alignment en you can not portably assume
> they will be the same.


To force the unsigned char array to have the same alignment as the Foo
(which is my goal), I'd like to put these together in a union.
However, Foo has a non-trivial constructor, so it may not be a member
of a union. Do I have any othre options?

Thanks to all!
 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      02-26-2009
On 26 Feb., 19:36, (E-Mail Removed) wrote:
> On Feb 26, 11:18*am, Gert-Jan de Vos <gert-
>
>
>
>
>
> (E-Mail Removed)> wrote:
> > On Feb 26, 7:00*pm, "(E-Mail Removed)"

>
> > <(E-Mail Removed)> wrote:
> > > Foo foo;
> > > unsigned char foo[sizeof(Foo)];

>
> > > Are these guaranteed to have the same alignment, or will the second
> > > definition have an alignment that is only modulo 1?

>
> > The standard has the answer in 3.1-5 [basic.types]:

>
> > "Object types have alignment requirements (3.9.1, 3.9.2). The
> > alignment of a complete object type is an implementation-defined
> > integer value representing a number of bytes; an object is allocated
> > at an address that meets the alignment requirements of its object
> > type."

>
> > So, unless your Foo is a typedef for an array of unsigned char, then
> > your foo objects are two different types. Therefore both have their
> > own implementation-defined alignment en you can not portably assume
> > they will be the same.

>
> To force the unsigned char array to have the same alignment as the Foo
> (which is my goal), I'd like to put these together in a union.
> However, Foo has a non-trivial constructor, so it may not be a member
> of a union. Do I have any othre options?


Yes. Search this group and the moderated one for a solution that will
give the correct alignment (basically: a union containing all possible
types and the char representation).

/Peter
 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      02-26-2009
On Feb 26, 10:36*am, (E-Mail Removed) wrote:
> On Feb 26, 11:18*am, Gert-Jan de Vos <gert-
>
> To force the unsigned char array to have the same alignment as the Foo
> (which is my goal), I'd like to put these together in a union.
> However, Foo has a non-trivial constructor, so it may not be a member
> of a union. Do I have any othre options?


Allocate the char array dynamically:

char * fooArray = new char[sizeof(Foo)];

Now fooArray will be suitably aligned for a Foo type.

Greg
 
Reply With Quote
 
better_cs_now@yahoo.com
Guest
Posts: n/a
 
      02-26-2009
On Feb 26, 12:11*pm, Greg Herlihy <(E-Mail Removed)> wrote:
> On Feb 26, 10:36*am, (E-Mail Removed) wrote:
>
> > On Feb 26, 11:18*am, Gert-Jan de Vos <gert-

>
> > To force the unsigned char array to have the same alignment as the Foo
> > (which is my goal), I'd like to put these together in a union.
> > However, Foo has a non-trivial constructor, so it may not be a member
> > of a union. Do I have any othre options?

>
> Allocate the char array dynamically:
>
> * *char * fooArray = new char[sizeof(Foo)];
>
> Now fooArray will be suitably aligned for a Foo type.
>
> Greg


Unfortunately, allocating on the heap is verboten in our application.
Whether it makes sense or not, it is what it is and it will not pass
the design review.

The memory I'm trying to get properly aligned is actually going to be
static in a class and will have a singleton instance of the class
constructed in it via placement new.

class Foo
{
...
static unsigned char s_pInstance[];
};

unsigned char Foo::s_pInstance[sizeof(Foo)]; // Will have a Foo
constructed in it

The goal is to ensure that s_pInstance is suitably aligned to hold a
Foo. Is this possible within the bounds of the C++ language standard,
or is it an architecture issue (in which case I'm in the wrong group)?
 
Reply With Quote
 
better_cs_now@yahoo.com
Guest
Posts: n/a
 
      02-26-2009
On Feb 26, 12:11*pm, Greg Herlihy <(E-Mail Removed)> wrote:
> On Feb 26, 10:36*am, (E-Mail Removed) wrote:
>
> > On Feb 26, 11:18*am, Gert-Jan de Vos <gert-

>
> > To force the unsigned char array to have the same alignment as the Foo
> > (which is my goal), I'd like to put these together in a union.
> > However, Foo has a non-trivial constructor, so it may not be a member
> > of a union. Do I have any othre options?

>
> Allocate the char array dynamically:
>
> * *char * fooArray = new char[sizeof(Foo)];
>
> Now fooArray will be suitably aligned for a Foo type.
>
> Greg


In addition to my prvious response a minute ago, your comment brings
up a side issue.

In my original post, I showed the Foo and the char array being
constructed off of the heap, and the response was that the alignment I
seek is not guaranteed. However, your response clearly states that the
alignment I seek *will* be guaranteed if they are constructed on the
heap. So, I'd like to reconfirm -- are alignment issues indeed treated
differently on the heap than off?

Thanks for the response!
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      02-26-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Feb 26, 12:11 pm, Greg Herlihy <(E-Mail Removed)> wrote:
>> On Feb 26, 10:36 am, (E-Mail Removed) wrote:
>>
>>> On Feb 26, 11:18 am, Gert-Jan de Vos <gert-

>>
>>> To force the unsigned char array to have the same alignment as
>>> the Foo (which is my goal), I'd like to put these together in a
>>> union. However, Foo has a non-trivial constructor, so it may not
>>> be a member of a union. Do I have any othre options?

>>
>> Allocate the char array dynamically:
>>
>> char * fooArray = new char[sizeof(Foo)];
>>
>> Now fooArray will be suitably aligned for a Foo type.
>>
>> Greg

>
> In addition to my prvious response a minute ago, your comment brings
> up a side issue.
>
> In my original post, I showed the Foo and the char array being
> constructed off of the heap, and the response was that the
> alignment I seek is not guaranteed. However, your response clearly
> states that the alignment I seek *will* be guaranteed if they are
> constructed on the heap. So, I'd like to reconfirm -- are alignment
> issues indeed treated differently on the heap than off?
>
> Thanks for the response!


No, the "trick" is the use of the global operator new,

void* operator new(std::size_t size) throw(std::bad_alloc);

which is required to allocate bytes "suitably aligned to represent any
object of that size".

Note that the operator doesn't know the type of the object, but must
still obey the rule. We don't know how!


Bo Persson




 
Reply With Quote
 
Gert-Jan de Vos
Guest
Posts: n/a
 
      02-26-2009
On Feb 26, 8:29*pm, (E-Mail Removed) wrote:
> On Feb 26, 12:11*pm, Greg Herlihy <(E-Mail Removed)> wrote:
> > Allocate the char array dynamically:

>
> > * *char * fooArray = new char[sizeof(Foo)];

>
> > Now fooArray will be suitably aligned for a Foo type.

>
> > Greg

>
> In addition to my previous response a minute ago, your comment brings
> up a side issue.
>
> In my original post, I showed the Foo and the char array being
> constructed off of the heap, and the response was that the alignment I
> seek is not guaranteed. However, your response clearly states that the
> alignment I seek *will* be guaranteed if they are constructed on the
> heap. So, I'd like to reconfirm -- are alignment issues indeed treated
> differently on the heap than off?


If you are after a singleton implementation that doesn't use the heap,
why not use Meyers singleton?

The standard specifically states that new array of char or unsigned
char provides correct alignment for any object that fits in the
requested size. This explicitly allows what you are after, except that
it needs heap storage.

 
Reply With Quote
 
better_cs_now@yahoo.com
Guest
Posts: n/a
 
      02-26-2009
On Feb 26, 12:50*pm, Gert-Jan de Vos <gert-
(E-Mail Removed)> wrote:
> On Feb 26, 8:29*pm, (E-Mail Removed) wrote:
>
>
>
>
>
> > On Feb 26, 12:11*pm, Greg Herlihy <(E-Mail Removed)> wrote:
> > > Allocate the char array dynamically:

>
> > > * *char * fooArray = new char[sizeof(Foo)];

>
> > > Now fooArray will be suitably aligned for a Foo type.

>
> > > Greg

>
> > In addition to my previous response a minute ago, your comment brings
> > up a side issue.

>
> > In my original post, I showed the Foo and the char array being
> > constructed off of the heap, and the response was that the alignment I
> > seek is not guaranteed. However, your response clearly states that the
> > alignment I seek *will* be guaranteed if they are constructed on the
> > heap. So, I'd like to reconfirm -- are alignment issues indeed treated
> > differently on the heap than off?

>
> If you are after a singleton implementation that doesn't use the heap,
> why not use Meyers singleton?
>
> The standard specifically states that new array of char or unsigned
> char provides correct alignment for any object that fits in the
> requested size. This explicitly allows what you are after, except that
> it needs heap storage.- Hide quoted text -
>
> - Show quoted text -


I had started with the Meyers singleton, but the problem I quickly ran
into was losing control over the sequence in which my singletons are
destructed. I have a lot of singletons, and there are dependencies
among them. So I must maintain control over their destruction order.
Having them constructed as static objects (i.e. as Meyers singletons)
deprives me of that control.
 
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
Regd---Justify option in Alignment. =?Utf-8?B?bWFudQ==?= ASP .Net 0 09-23-2005 07:35 AM
Phase alignment ALuPin VHDL 0 05-17-2004 09:12 AM
Closing a browser, crystal report viewer alignment Anne ASP .Net 0 07-30-2003 02:49 PM
Crystal Reports Viewer alignment Anne ASP .Net 0 07-29-2003 03:34 PM
Re: Help required for alignment problem Sunil Menon ASP .Net 0 06-26-2003 07:56 AM



Advertisments