C
CJ
Has it occurred to you that there are environments/situations where
you are more interested in getting the program to RUN is much
more important than writing it portably?
I have worked in DSPs where there was only 80K RAM, and of that 70K
was used by the modem software.
I had to run in 10K.
Or, you have to layout your structure according to EXTERNAL
requirements like passing it character-wise through a
serial interface, and at the other side you can't assume any
"alignment" wasted space, etc etc!
One of the main advantages of writing in C is that it is widely
portable. If you don't care about portability, then sure, write in
C+extensions and drop down to assembly if you want. The fact that people
sometimes need to write unportable code doesn't justify polluting
standard C with obscure implementational details.
You are mistaken.
struct foo {
char a;
double b;
char c;
};
The double will be aligned (in many machines) into an 8 byte
boundary
This example does not contradict in any way what I wrote.
Obviously the compiler has to see the declaration of the structure, so
it is imperative to put everything related to it in the declaration.
This poses no problem at all if you write your structure declarations
in header files...
And if you happen not to follow this best practice, having a function
expecting to receive an unpacked struct and actually getting a packed
struct could lead to all sorts of subtle and hard-to-find bugs. Yuck!