Re: Array assignment via struct



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.
.



Relevant Pages

  • Re: Array assignment via struct
    ... Structures don't have alignment requirements, ... What are the alignment requirements of a char array? ... So a structure holding a character array would ...
    (comp.lang.c)
  • Re: Array assignment via struct
    ... You're trying to access an array as a structure which ultimately has ... t;}*) b); but the compiler complained about incompatible types, ... All of S is an array of char. ... Structures don't have alignment requirements, ...
    (comp.lang.c)
  • Re: Array assignment via struct
    ... > All of S is an array of char. ... > alignment requirements, their members do. ... So a structure holding a character array would ...
    (comp.lang.c)
  • Re: Question about char() arrays in parameters
    ... > For example, I'm using the String's TrimEnd function, which takes in a char ... > array as a parameter. ... > there anyway to denote this as a character array, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Question about char() arrays in parameters
    ... > strict. ... > For example, I'm using the String's TrimEnd function, which takes in a char ... > array as a parameter. ... > there anyway to denote this as a character array, ...
    (microsoft.public.dotnet.languages.vb)