|
-
September 11th, 2002, 05:42 AM
#1
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|