Re: Need clarity on structure alignment



Keith Thompson wrote:
"Tony" <tony@xxxxxx> writes:
"Keith Thompson" <kst-u@xxxxxxx> wrote in message news:lnk58g7d68.fsf@xxxxxxxxxxxxxxxxxx
[...]
Alignment requirements for different types vary depending on the
underlying hardware, and on what the compiler chooses to enforce.
I remember seeing something that left me with the impression that C (C++?) guarantees that a struct will be aligned on a pointer-size boundary (using the std mem mgr). (?) Something about 4-byte struct alignment (on a 32-bit machine no doubt) sticks in my mind.

You remember incorrectly. There's no rule in C (or C++) about the
relationship between the alignment requirements for structs and
those for pointers. You may have seen something about a specific
implementation.

The phrase "using the std mem mgr" suggests he may have
been thinking of malloc(), in which case he's sort of right:
If he uses malloc() to obtain memory for a struct, the memory
will in fact be properly aligned for storing a pointer. (Also
for storing a double, a union, a va_list, a time_t, and a
partridge in a pear tree, of course: malloc() memory is properly
aligned for *any* kind of data.)

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Malloc statistics patch
    ... Now that the UMA changes to use critical sections instead of per-cpu mutexes ... memory allocation patch previous posted. ... - Introduce per-cpu malloc type statistics, ... - If we're willing to abandon 5.x backport, we can clean up 'struct ...
    (freebsd-performance)
  • Re: The annotated annotated annotated C standard part 3
    ... Beautiful Code, written in 1990, for memory allocation) is destroyed ... When the ability to use language is so checked by the ... You could avoid a problem I understand exists in malloc(), ... the caller to pass an instance of this struct. ...
    (comp.programming)
  • Re: A malloc question
    ... reliably store 100 chars followed by a struct in this memory, ... the alignment of p+100 may not be suitable for the struct. ... the paddings for memory alignment are ... malloc() will see a request of 24 byte allocation. ...
    (comp.lang.c)
  • Re: The annotated annotated annotated C standard part 3
    ... Beautiful Code, written in 1990, for memory allocation) is destroyed ... When the ability to use language is so checked by the ... You could avoid a problem I understand exists in malloc(), ... the caller to pass an instance of this struct. ...
    (comp.programming)
  • Re: Need clarity on structure alignment
    ... guarantees that a struct will be aligned on a pointer-size ... If he uses malloc() to obtain memory for a struct, ...
    (comp.lang.c)