Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. c++ and external structs [modified]

c++ and external structs [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    earl
    wrote on last edited by
    #1

    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 or unsigned 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 = 5BAE25

    file2.cpp:

    isDebugMode = 1, address = 5BAE20
    aspectRatio = 876814592, address = 5BAE24
    canary = 12, address = 5BAE28

    file1.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

    M 1 Reply Last reply
    0
    • E earl

      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 or unsigned 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 = 5BAE25

      file2.cpp:

      isDebugMode = 1, address = 5BAE20
      aspectRatio = 876814592, address = 5BAE24
      canary = 12, address = 5BAE28

      file1.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

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups