Re: Array assignment via struct
- From: Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx>
- Date: 07 Aug 2005 00:29:29 -0700
Joe Wright <jwright@xxxxxxxxxxx> writes:
> Tim Rentsch wrote:
> > Joe Wright <jwright@xxxxxxxxxxx> writes:
> >
> >
> >>All of S is an array of char. What alignment requirements
> >>might there be for an S? None. Structures don't have
> >>alignment requirements, their members do. What are the
> >>alignment requirements of a char array?
> >
> >
> > Structures can have alignment requirements that
> > are different from those of their members, and
> > here's a possible reason why they would.
> >
> > On platforms where a 'char *' is a different
> > format and/or wider than an 'int *', an
> > implementation might choose to make all structs
> > be 'int' aligned, so that structure pointers
> > would be easier to deal with.
> >
> > So a structure holding a character array would
> > still need 'int' alignment, even though the
> > contained character array would need only 'char'
> > alignment.
> >
> > The alignment requirement also implies a sizing
> > requirement, since alignment_of(T) must evenly
> > divide 'sizeof(T)'. That's why a struct that
> > holds only a character array might be bigger
> > than the character array it holds.
>
> You just made all that up didn't you?
In fact, I didn't. I read about such platforms here
in comp.lang.c.
For example, consider a machine that addresses 64-bit
words natively. Pointers and ints are both 64 bits,
and use word addresses. A 64-bit word holds 8
eight-bit char's; a pointer to char uses a word
address, but puts the three bits that indicate which
char within the word in the high order bits of the
64-bit pointer. I'm doing this from memory, so I may
have some of the details wrong; however, other people
have written about C implementations on actual machines
that are very much like this.
It would be very natural on such a machine to have
all struct's be multiples of 8 in size, and aligned
on word boundaries.
.
- Follow-Ups:
- Re: Array assignment via struct
- From: Keith Thompson
- Re: Array assignment via struct
- References:
- Array assignment via struct
- From: Eric Laberge
- Re: Array assignment via struct
- From: Lawrence Kirby
- Re: Array assignment via struct
- From: Joe Wright
- Re: Array assignment via struct
- From: Jack Klein
- Re: Array assignment via struct
- From: Joe Wright
- Re: Array assignment via struct
- From: Tim Rentsch
- Re: Array assignment via struct
- From: Joe Wright
- Array assignment via struct
- Prev by Date: Re: Learning C?
- Next by Date: Re: find out the problem
- Previous by thread: Re: Array assignment via struct
- Next by thread: Re: Array assignment via struct
- Index(es):
Relevant Pages
|