Help:: Cross Platform Struct Member Alignment
I am building a windows gui app to manage a database. The database is created and runs on a unix system, and I have tools written in pure C that I use to manipulate the database from the command line, but I wanted a gui app to make things more user friendy. The tools I have written when compiled either by VC6 as a C program, gcc on linux, or cygwin all work fine. Transfering the database manipulation code to a VC6 project and compiling as part of a C++ gui app results in the structures not being able to be read due to improper bit field alignment within the structures.
In short, the database can be read/written via a C compiled source, but not via C++ compiled source.
When determining structure size - printf("d", sizeof(struct my_struct));
In a C program it prints as 3524 bytes. I assume that most C compilers align the bit fields in a 4 bit structure.
In a C++ program it prints the same structure as 3508 bytes, and that's assuming that the compiler uses an 8 bit structure alignment.
I have tried the /ZP(n) compiler switch, as well as the #pragma pack(...) directives. The min /ZP1 results in a struct size of 3501 bytes, while the /ZP4, /ZP8, and max /ZP16 all result in a struct size of 3508 bytes. I have also tried the /J compiler switch to set char alignment, and the UNALIGNED keyword when declaring structure pointers, all of these methods failed to align the structure bit fields to a readable state by a C++ program.
I assumed that since the original program and database is completely portable C programming, that I could also port it to C++. Is there anything that I am missing, or is this just impossible to do?
Thanks...
Pardon me if I didn't read the whole passage...
But what type of packing does the structure used in the database have?
If you can rewrite both apps - the standard way of controlling structure packing is via the <pshpackX.h> header file where X is defined to be the packing alignment. Of course, include <poppack.h> to set back to default alignment.
#include <pshpack1.h>
struct WHATEVER {
// ....
};
#include <poppack.h>
Re: Pardon me if I didn't read the whole passage...
Quote:
Originally posted by JamesSchumacher
But what type of packing does the structure used in the database have?
If you can rewrite both apps - the standard way of controlling structure packing is via the <pshpackX.h> header file where X is defined to be the packing alignment. Of course, include <poppack.h> to set back to default alignment.
#include <pshpack1.h>
struct WHATEVER {
// ....
};
#include <poppack.h>
I don't have the option of re-writing the old app (it's 10's of thousands of lines of code), nor do I have the option of re-creating the database it uses. I just wanted to access the database through C++, which now with the typedef change that Stober picked up on and some minor structure packing changes I am now able to do.
Thank you all for your help. :)