c++ and external structs [modified]
-
So, here's the issue:
//******globals.h
#ifndef ___GLOBALS_H___
#define ___GLOBALS_H___
//the aspect ratio
enum AspectRatio {
Aspect4to3,
Aspect5to3,
Aspect5to4,
AspectOther
};/* the global state struct; throw all globals in here */
struct GState
{
bool isDebugMode;
//align: char a,b,c; //unsigned int a:24; //align
AspectRatio aspectRatio;
unsigned int canary;
};extern struct SState gState;
#endif
and for the declaration:
//***** globals.cpp
#include"globals.h"SState gState;
Now, the problem: this is being compiled with VC6SP5 (yes, I know, there's nothing I can do for the time being). I manually checked that all files are being compiled with the same flags. In most files, I include globals.h and everything is kosher. In *some* files, the address of aspectRatio is bumped by 3. I can't figure out what this could possibly be except some weird alignment issue. Solutions: if I uncomment char a,b,c;, then everything works. If I put unnamed (or named) bitfields in, either
unsigned int : 0
orunsigned int:24
(the former promises to align everything that follows on an int), it's still broken. So, for example, in one file,printf("isDebugMode = %u, address = %X \naspect ratio = %u, address = %X \n"
"canary=%X, add = %X",
gState.isDebugMode, &(gState.isDebugMode),
gState.aspectRatio, &(gState.aspectRatio),
gState.canary, &(gState.canary));canary is being set to 0x12344321 results in: file1.cpp:
isDebugMode = 1, address = 5BAE20
aspectRatio = 2, address = 5BAE21
canary = 12344321, address = 5BAE25file2.cpp:
isDebugMode = 1, address = 5BAE20
aspectRatio = 876814592, address = 5BAE24
canary = 12, address = 5BAE28file1.cpp is correct; file2.cpp isn't. The address for aspectRatio is being bumped by 3. Can anyone tell me what the hell is going on? Thanks, earl PS: struct member alignment is set to 8 bytes in the compilation flags. -- modified at 20:27 Monday 3rd July, 2006
-
So, here's the issue:
//******globals.h
#ifndef ___GLOBALS_H___
#define ___GLOBALS_H___
//the aspect ratio
enum AspectRatio {
Aspect4to3,
Aspect5to3,
Aspect5to4,
AspectOther
};/* the global state struct; throw all globals in here */
struct GState
{
bool isDebugMode;
//align: char a,b,c; //unsigned int a:24; //align
AspectRatio aspectRatio;
unsigned int canary;
};extern struct SState gState;
#endif
and for the declaration:
//***** globals.cpp
#include"globals.h"SState gState;
Now, the problem: this is being compiled with VC6SP5 (yes, I know, there's nothing I can do for the time being). I manually checked that all files are being compiled with the same flags. In most files, I include globals.h and everything is kosher. In *some* files, the address of aspectRatio is bumped by 3. I can't figure out what this could possibly be except some weird alignment issue. Solutions: if I uncomment char a,b,c;, then everything works. If I put unnamed (or named) bitfields in, either
unsigned int : 0
orunsigned int:24
(the former promises to align everything that follows on an int), it's still broken. So, for example, in one file,printf("isDebugMode = %u, address = %X \naspect ratio = %u, address = %X \n"
"canary=%X, add = %X",
gState.isDebugMode, &(gState.isDebugMode),
gState.aspectRatio, &(gState.aspectRatio),
gState.canary, &(gState.canary));canary is being set to 0x12344321 results in: file1.cpp:
isDebugMode = 1, address = 5BAE20
aspectRatio = 2, address = 5BAE21
canary = 12344321, address = 5BAE25file2.cpp:
isDebugMode = 1, address = 5BAE20
aspectRatio = 876814592, address = 5BAE24
canary = 12, address = 5BAE28file1.cpp is correct; file2.cpp isn't. The address for aspectRatio is being bumped by 3. Can anyone tell me what the hell is going on? Thanks, earl PS: struct member alignment is set to 8 bytes in the compilation flags. -- modified at 20:27 Monday 3rd July, 2006
Put a
#pragma pack
instruction around the struct definition. The compiler option just sets the default packing, some other buggy header may be changing the packing and not resetting it.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer