Structure packing

I had the same problems with the member allignment - Sam should surround

structure definitions with:
#ifdef VISUALC
#pragma pack (8)

Will do. Can you send me an example header?

Sure, this is from SDL_events.h

#ifndef _SDL_events_h
#define _SDL_events_h

/* this is new /
#ifdef _MSC_VER
#pragma pack(push,8)
#endif /

#ifdef __cplusplus
extern “C” {

#include “SDL_defun.h”
#include “SDL_types.h”
#include “SDL_active.h”

#ifdef __cplusplus

/* this is new too /
#ifdef _MFC_VER
#pragma pack(pop)
#endif /

#endif /* _SDL_events_h */

That should be needed in every header file that defines structures
that are returned by or passed to dll functions because different member
allignments will result in crashes.
However, I don’t know what allignment setting gcc for Windows uses (dont
have it). You can change the 8 in #pragma pack(push,8) to that value.

— Paulus Esterhazy (@Paulus_Esterhazy)

Is there a test program I can run that will tell me? How do I find out?
I’d like binaries compiled by one to be usable by the other…
Please let me know ASAP, because that will be the first patch to be applied
to SDL 0.8 since it affects binary compatibility.

OK, compile this:

#include <stdio.h>

struct testen {
char a;
int b;
short c;
char d;
int e;
short f,g;
int h;
char j;

void main()
printf(“Size of struct testen is %d\n”, sizeof(testen) );

If this shows 28 with gcc/win32, then 8 was right and you can
use #pragma pack(push,8). That should make it binary compatible.
I’ve tried DJGPP, Watcom C and Visual C, they’re all using 8 as
default so I’d say that’s it but better try it.

— Paulus Esterhazy (@Paulus_Esterhazy)

What is the output of this when compiled with VC++?
It’s the same. It seems that 8-byte isn’t really qword
but dword boundaries (qword boundaries would really waste too
much space). I’ve noticed DGJPP doesn’t support the 8
byte setting and all compilers I tried produced with 4 byte
the same as with 8 byte so it shouldn’t matter anyway.

I suggest you just do
#pragma pack(4) /* set 4 byte member allignment boundaries /
at the beginning of structure definitions and
#pragma pack() /
restore default setting */
at the end because only Visual-C supports #pragma pack(push…).

My egcs for linux and djgpp support #pragma pack so gcc/win32 certainly
supports it, too so #ifdefs aren’t needed (unknown #pragmas are ignored).
Note that you must not #include files between the pragmas because
if this file does the same thing, it restores the default setting at the end
and it wasn’t any use.
That should finally solve the problem because when the libraries are
compiled with the same setting as the application, it works anyhow
no matter if compiled with visualc or gcc.

i’m sorry that I got this right so late but I had to figure it out myself.
packing is an annoying problem, especially when loading data
files from disk or with dlls.
:slight_smile: — Paulus Esterhazy

What is the output of this when compiled with VC++?
It’s the same.

If the output of that program under VC++ and gcc are the same, why are
the structures not binary compatible?
They are binary compatible and they’re even already binary compatible at the
moment and I did run
the program that outputs the member offsets. It’s just that if you run a
compiler with a different
default member allignment, it doesn’t work without the pragmas. This was
actually the mistake
I made the first time because it can be quite useful change the default
setting when for programs
with data structures so you don’t have to pragma in every file.
Visual C does #pragmas packs in every (for example direct x) header file.
It is not extremly important, but quite a nice thing to do 'cause with it,
you’re on the safe side.

— Paulus Esterhazy