"Alexei Alexandrov" <[EMAIL PROTECTED]> wrote:
> >
> > Perhaps the following definition of Mem would work better:
> >
> > struct Mem {
> > u16 flags;
> > u8 type;
> > u8 enc;
> > char *z;
> > int n;
> > i64 i;
> > double r;
> > char zShort[NBFS];
> > void (*xDel)(void *);
> > };
> >
>
> Not exactly, since 'int' is still 4 bytes on Linux ia64. long/size_t
> is 8 bytes. In fact, I think that the rightest thing here would be to
> put zShort at the beginning of the structure, because in this case it
> would get the same alignment as returned from malloc and the rest of
> fields are strictly typed so that they are aligned by compiler
> properly.
>
> But when I try to put zShort at the beginning, some strange thing
> happens - SQLite doesn't like it. I start SQLite shell, and it says
> immediately (or when I create a simple table):
>
Come to think of it, I there are some places in the code that
assume that zShort[] is at the end of the structure. There
are places that memcpy() the first part of the structure and
ignore zShort[]. So moving zShort[] to any other place in the
structure will not work unless those places are changed. And
those places are there for efficiency reasons so I am reluctant
to change them.
Perhaps a better fix is this:
struct Mem {
i64 i;
double r;
char *z;
int n;
u16 flags;
u8 type;
u8 enc;
void (*xDel)(void *);
union {
long double notUsed1;
char zShort[NBFS];
};
};
The compiler would then (hopefully) insert an appropriate
amount of padding prior to zShort so that it had the same
alignment restructions as a long double.
This approach uses an anonymous union, which I confess is
a C construct that I have never in 22 years of C programming
had the occasion to employ. It seems to work well enough
using gcc 3.3.4. But down in my gut I have this nagging
fealing that it will likely break on some compilers.
--
D. Richard Hipp <[EMAIL PROTECTED]>