Re: Structure size
On Jul 20, 4:27 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
LR wrote:
[...]
(I'm assuming that as you said in another post, you don't have a
portability problem and you can control your compiler to take care of
packing issues.)
struct x1 {
int a,b,c;
};
struct x2 {
int a; double e,f,g,h,i;
};
template<typename T>
struct Message {
T t;
char v[1024 - sizeof(T)];
};
then, else where you can...
Message<x1> m1;
Message<x2> m2;
there will be the added annoyance of having to reference the
individual members of x1 and x2 by doing something like m1.t.a = 43;
It might be wise to pick a better name for t.
[..]
I am not sure here, but I believe there is no guarantee that the size
of the 'Message<x1>' or 'Message<x2>' is actually going to be 1024
even with the mechanism you give here.
It's certainly not guaranteed by the standard. A compiler could
insert padding between the t and v, and still be conform. As a
quality of implementation issue... I would consider it a poor
choice on a byte addressed machine (and none of the compilers
I've ever used did insert padding here).
I wonder, however, if there
is something in the Standard related to the sizes (and alignments)
that I missed, and it might actually impose certain limitations thus
giving programmers more leeway when it comes to determining type sizes.
I don't think so.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34