B
Ben Bacarisse
Richard said:Which implementation?
The C compiler for the PERQ did this if memory serves, but it was a
long time ago.
Richard said:Which implementation?
Richard said:Which implementation?
Alec S. said:You know, in all the discussion that has been going on in this thread, I did not
see anyone give one of the most obvious reason for caring about padding: games
development. Any console dev will tell you that it’s important to consider data
alignment and padding when designing your data, especially since you have a
particilar platform with specific specs to code for. Not only do you have a set
amount of memory to work with, but you can perform several kinds of
optimizations if the data is structured right. You can reduce the number of
reads, stream data faster, process data more efficiently, and some hardware even
requires certain alignments for some things.
I think I’ve seen the #pragma pack directive before, but another way to get some
control over alignment is with __declspec(align(x)).
Last month (December 2008)’s issue of Game Developer Mag had an article on just
this thing. Noel has provided an interesting pair of functions called
aligned_malloc and aligned_free that you can use to allocate and free variables
according to a specified alignment by controling (and automating) the padding.
I guess there aren’t any game devs here. :|
Alec said:You know, in all the discussion that has been going on in this thread, I did not
see anyone give one of the most obvious reason for caring about padding: games
development. Any console dev will tell you that it’s important to consider data
alignment and padding when designing your data, especially since you have a
particilar platform with specific specs to code for. Not only do you have a set
amount of memory to work with, but you can perform several kinds of
optimizations if the data is structured right. You can reduce the number of
reads, stream data faster, process data more efficiently, and some hardware even
requires certain alignments for some things.
I think I’ve seen the #pragma pack directive before, but another way to get some
control over alignment is with __declspec(align(x)).
I guess there aren’t any game devs here. :|
Nothing in the OP's postings relate to alignment performance, they only
relate to his limited ability to visualise structure padding.
Unfortunately, C/C++ compilers are not allowed to dig into and
reorder your structs. After some years of development, or some
coworkers with an overactive sense of 'style' and grouping, a lot of
structs could be reduced in size. I'd love to have an (optional)
warning that'd say something like C###: struct has N bytes of padding,
but could be reordered to have only M padding bytes. Compilers are
treated as black boxes, but with a little bit of verbosity, useful
feedback can be generated.
struct check_struct
{
double a;
char b;
char reserved[7];
If you do that you can see the padded bytes.
I don't think so. It will always show you the reserved member's
contents but I think the implementation is still free to insert
padding in any amount and at any location in this structure it
pleases, except at the beggining.<
I dare say, in principle, but I use compiler options which specify the
padding properties, and it would be unusual for 8 byte boundaries not to be
safe.
[I have large arrays of structures with many bit fields, and it is in my
interest, to arrange them so that padding is unnecessary, and so I control
very carefully where the bit fields fit in relation to byte and word
boundaries. It's a useful principle of "defensive programming".]
Dave
You know, in all the discussion that has been going on in this thread, I did not
see anyone give one of the most obvious reason for caring about padding: games
development. Any console dev will tell you that it’s important to consider data
alignment and padding when designing your data, especially since you have a
particilar platform with specific specs to code for. Not only do you have a set
amount of memory to work with, but you can perform several kinds of
optimizations if the data is structured right. You can reduce the number of
reads, stream data faster, process data more efficiently, and some hardware even
requires certain alignments for some things.
I think I’ve seen the #pragma pack directive before, but another way to get some
control over alignment is with __declspec(align(x)).
Last month (December 2008)’s issue of Game Developer Mag had an article on just
this thing. Noel has provided an interesting pair of functions called
aligned_malloc and aligned_free that you can use to allocate and free variables
according to a specified alignment by controling (and automating) the padding.
I guess there aren’t any game devs here. :|
Nathan Mates wrote (innews:[email protected]):
I like that idea, and it could probably be fairly easy to implement as well.
I used the term "unless you're programming a toaster" as a shorthand
for any resource constrained embedded system. Eg. a toaster or
a games console.
Even embedded code sometimes has to be surprisingly portable.
Those darn hardware engineers keep building new chips!
David said:Indeed, but it helps me know about it.
likely alignment restrictions when deciding on the order of fields and I
would agree with you that the following is BAD
struct bad {
double a;
char b;
double c;
}
Please explain what's bad about that (aside from the missing semicolon
following the type definition). Only in the unlikely circumstance that
sizeof (double) < sizeof (char).... and sizeof (char) is defined as 1 I'm
told. Any ordering of those three variables is likely to end up with
sizeof (bad) == 3 * sizeof (double) on architectures with alignment
restrictions.
Interchange char and double and now you have an inefficient layout.
With the given ordering, and an alignment requirement for the double
precision values, the compiler would have to insert padding after the char
element.
OTOH, with
struct alternative {
double a;
double c;
char b;
};
it is likely that no padding would be required after the char element.
Lew Pitcher said:With the given ordering, and an alignment requirement for the double
precision values, the compiler would have to insert padding after the char
element.
OTOH, with
struct alternative {
double a;
double c;
char b;
};
it is likely that no padding would be required after the char element.
Thus, the first ordering results in a structure that is larger than
necessary (as compared to the 2nd ordering).
OTOH, with
struct alternative {
double a;
double c;
char b;
};
it is likely that no padding would be required after the char element.
Lew Pitcher said:[...]
With the given ordering, and an alignment requirement for the double
precision values, the compiler would have to insert padding after the char
element.
OTOH, with
struct alternative {
double a;
double c;
char b;
};
it is likely that no padding would be required after the char element.
Thus, the first ordering results in a structure that is larger than
necessary (as compared to the 2nd ordering).
Keith Thompson said:[I'm temporarily not obeying the followup to comp.lang.c, since I'm
correcting an error that readers in the other newsgroup might be
interested in. I've redirected followups for this article to
comp.lang.c.]
Lew Pitcher said:[...]struct bad {
double a;
char b;
double c;
}
With the given ordering, and an alignment requirement for the double
precision values, the compiler would have to insert padding after the char
element.
Conceivably it could move b to the end of the word and insert padding
before it; that's permitted, but unlikely.
James said:Lew Pitcher wrote:
...
The structure size has to be a multiple of the alignment requirements of
any of it's members; otherwise members of the second element of an array
of "struct alternative" would be incorrectly aligned. Therefore, it
would still need padding.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.