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. Other Discussions
  3. The Weird and The Wonderful
  4. Misaligned elephants

Misaligned elephants

Scheduled Pinned Locked Moved The Weird and The Wonderful
c++question
26 Posts 16 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.
  • Y YvesDaoust

    Matthew, it is hard to believe that you get different behaviors with all build options being identical. Shouldn't you double check that ? It is also hard to understand the value 16. shorts are never 8 bytes long, are they ? Could it be a struct member alignment option silently set in a preceding header file ?

    M Offline
    M Offline
    Matthew Faithfull
    wrote on last edited by
    #9

    YvesDaoust wrote:

    Could it be a struct member alignment option silently set in a preceding header file

    I suspect it might be just that, but leaking from a Windows header. Including pulls in which in this case is a project header not a system header. My guess is that the string support code is being included in the middle of a windows header section that for some reason has different alignment options. Tracking it down or doing anything about it would be rather difficult though. I've worked round this one but I'll be on the lookout next time and also need to get pragma pack support into my Compiler support library as soon as I can.

    "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

    R 1 Reply Last reply
    0
    • M Matthew Faithfull

      Thanks for the example. The answer is that I'm shooting for as wide a compiler compatibility as possible. MSVC, Intel, MinGW, GCC, Clang, Borland/Rad studio, Open Watcom, DMC. Everything has to work on MSVC and GCC to start with and the rest where possible. At the moment my Compiler support library [^]doesn't provide unified pragma pack support across compilers largely because documentation on GCC pragma's is either unreliable or non existent, you're officially supposed to 'read the code' but the question of which part of the code remains a mystery unsolved by greping or Googling. It's a mystery I will eventually need to solve as I'm sure that pretty much every C++ compiler provides packing support somehow and there are times when it's really needed.

      "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #10

      No worries. I had wondered if I might not get an answer with considerably wider scope than my example. I just checked through zlib an cxImage for examples of its use. No luck with zlib and only limited examples of it in cxImage. cxImage also claims its "tested with Visual C++ 6 / 2008, C++ Builder 3 / 6, MinGW on Windows, and with gcc 3.3.2 on Linux." (http://www.xdp.it/cximage.htm[^])Since it deals with image header structs, I figured it'd be a good place to go looking for #pragma pack I found it, but only in the MSVC format (i.e #pragma pack(n) and #pragma pack() pairs ) All David's done is to wrap all of the structs needing byte alignment into a single pair of pack statements. From bitter experience, I know that bitmap handling can explode spectacularly when the struct packing isn't considered. With that in mind, and if you've the time, I'd be 1/2 way tempted to try to build it with each platform you're interested in. Though surely, there must be an easier and less labour-intensive way to find out. Whether or not you'd have the answer sooner though, I'm not prepared to wager on. ;)

      Make it work. Then do it better - Andrei Straut

      1 Reply Last reply
      0
      • M Matthew Faithfull

        If have a class containing this snippet declared in a C++ header file.

        protected:
        
            \_tChar\* m\_p;
            
            //--------------------------------------------------------------------------------
            struct sHeader
            {
                unsigned short usAlloc;
                unsigned short usLen;
            };
        
            //--------------------------------------------------------------------------------
            struct sFooter
            {
                unsigned short usRefCount;
            };
        
            //--------------------------------------------------------------------------------
            virtual unsigned short HeaderByteSize( void ) const
            {
                return sizeof( sHeader );
            }
        

        The header is used by half a dozen Dlls which are all part of a project. Instances of the class are created and passed around freely except it doesn't work. Because one dll has decided that sizeof( sHeader ) is 4 bytes as expected and another has decided that it's 16. The same header file, same build options and everything built together in a clean build. :mad::mad: The whole point of using sizeof( sHeader ) in the first place instead of just 4 was so that alignment wouldn't screw thing up. :mad::mad::mad: Now what do I do?

        "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

        S Offline
        S Offline
        SortaCore
        wrote on last edited by
        #11

        Maybe it's my bad experience with headers, but I'm immediately suspicious of the _tChar's type...

        1 Reply Last reply
        0
        • enhzflepE enhzflep

          I'm more than a little curious as to which compilers you're working with, given that #pragma pack works for both VC and GCC, albeit with slightly different syntax. I'm not in a position to test VC just now, but couldn't you simply add to wrap each of the compiler-specific packing semantics, in much the same way as programs designed to compile under vc, mingw and *nix gcc? I'd be tempted to use something like the following (Can't remember and too lazy to check compiler defines, hence the manual compiler specification)

          #define mingw 1
          //#define msvc 1

          #ifdef mingw
          #pragma pack(push,1)
          #endif

          #ifdef msvc
          #pragma pack (1)
          #endif
          typedef struct tagBITMAPFILEHEADER {
          WORD bfType;
          DWORD bfSize;
          WORD bfReserved1;
          WORD bfReserved2;
          DWORD bfOffBits;
          } BITMAPFILEHEADER,*LPBITMAPFILEHEADER,*PBITMAPFILEHEADER;
          #ifdef mingw
          #pragma pack(pop)
          #endif

          #ifdef msvc
          #pragma pack()
          #endif // VC

          Make it work. Then do it better - Andrei Straut

          G Offline
          G Offline
          Gary Wheeler
          wrote on last edited by
          #12

          FYI: http://msdn.microsoft.com/en-us/library/2e70t5y1(v=vs.71).aspx[^] #pragma pack(push,_n_) has been supported in Visual C++ since VS2003.NET.

          Software Zen: delete this;

          1 Reply Last reply
          0
          • M Matthew Faithfull

            Thanks for the example. The answer is that I'm shooting for as wide a compiler compatibility as possible. MSVC, Intel, MinGW, GCC, Clang, Borland/Rad studio, Open Watcom, DMC. Everything has to work on MSVC and GCC to start with and the rest where possible. At the moment my Compiler support library [^]doesn't provide unified pragma pack support across compilers largely because documentation on GCC pragma's is either unreliable or non existent, you're officially supposed to 'read the code' but the question of which part of the code remains a mystery unsolved by greping or Googling. It's a mystery I will eventually need to solve as I'm sure that pretty much every C++ compiler provides packing support somehow and there are times when it's really needed.

            "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

            S Offline
            S Offline
            Sentenryu
            wrote on last edited by
            #13

            I'm the only one who thinks that if i've to read the code of the tools I use to be able to use it, i've no reason to use the tool? seriously, i hate when people pull open source to justify their lack of documentation.

            I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

            M B 2 Replies Last reply
            0
            • M Matthew Faithfull

              If have a class containing this snippet declared in a C++ header file.

              protected:
              
                  \_tChar\* m\_p;
                  
                  //--------------------------------------------------------------------------------
                  struct sHeader
                  {
                      unsigned short usAlloc;
                      unsigned short usLen;
                  };
              
                  //--------------------------------------------------------------------------------
                  struct sFooter
                  {
                      unsigned short usRefCount;
                  };
              
                  //--------------------------------------------------------------------------------
                  virtual unsigned short HeaderByteSize( void ) const
                  {
                      return sizeof( sHeader );
                  }
              

              The header is used by half a dozen Dlls which are all part of a project. Instances of the class are created and passed around freely except it doesn't work. Because one dll has decided that sizeof( sHeader ) is 4 bytes as expected and another has decided that it's 16. The same header file, same build options and everything built together in a clean build. :mad::mad: The whole point of using sizeof( sHeader ) in the first place instead of just 4 was so that alignment wouldn't screw thing up. :mad::mad::mad: Now what do I do?

              "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

              R Offline
              R Offline
              rbocquier
              wrote on last edited by
              #14

              May be you can try to print out actual alignment settings with #pragma pack(show) If this is indeed the problem then you'll search where and how it's set

              M 1 Reply Last reply
              0
              • R rbocquier

                May be you can try to print out actual alignment settings with #pragma pack(show) If this is indeed the problem then you'll search where and how it's set

                M Offline
                M Offline
                Matthew Faithfull
                wrote on last edited by
                #15

                Nice I didn't know a show option existed, that will get added to my Compiler support diagnostics, thanks :)

                "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                1 Reply Last reply
                0
                • S Sentenryu

                  I'm the only one who thinks that if i've to read the code of the tools I use to be able to use it, i've no reason to use the tool? seriously, i hate when people pull open source to justify their lack of documentation.

                  I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

                  M Offline
                  M Offline
                  Matthew Faithfull
                  wrote on last edited by
                  #16

                  I very much agree. Open Source is not Open unless you can read it but a tool is no use as a tool if you have to fix it first before you can use it. In fairness most of GCC is reasonably or at least minimally documented, pragmas are an exceptionally bad area.

                  "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                  S 1 Reply Last reply
                  0
                  • M Matthew Faithfull

                    I very much agree. Open Source is not Open unless you can read it but a tool is no use as a tool if you have to fix it first before you can use it. In fairness most of GCC is reasonably or at least minimally documented, pragmas are an exceptionally bad area.

                    "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                    S Offline
                    S Offline
                    Sentenryu
                    wrote on last edited by
                    #17

                    I'm trying to work out how to use the SharpDX toolkit, even the documented areas have really poor descriptions of what they do and what they need... I'm starting to think i should just learn low level DirectX (I plan to, but once i've a high level understanding) Things like that make me think that open source means "Here's my code, you can use it, but you need to fix it first."

                    I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

                    M R 2 Replies Last reply
                    0
                    • S Sentenryu

                      I'm trying to work out how to use the SharpDX toolkit, even the documented areas have really poor descriptions of what they do and what they need... I'm starting to think i should just learn low level DirectX (I plan to, but once i've a high level understanding) Things like that make me think that open source means "Here's my code, you can use it, but you need to fix it first."

                      I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

                      M Offline
                      M Offline
                      Matthew Faithfull
                      wrote on last edited by
                      #18

                      This is often the case. I don't mind too much except when I go to fix the code and find it is an unformatted mess with little structure that is a pain in the eyes to read. Then I usually go and write my own. With DirectX I would say it depends how much COM you know. If you're happy with COM principles then just go straight to DirectX it's not at all difficult. If you hate or don't know COM then I would probably use with the toolkit unless it is hopeless.

                      "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                      1 Reply Last reply
                      0
                      • S Sentenryu

                        I'm the only one who thinks that if i've to read the code of the tools I use to be able to use it, i've no reason to use the tool? seriously, i hate when people pull open source to justify their lack of documentation.

                        I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

                        B Offline
                        B Offline
                        BiggerDon
                        wrote on last edited by
                        #19

                        Your English is as good as many of the programmers I know who speak it as it's first language...and as I tell people who complain about understanding people who speak/write English as a second/third/fourth... language, "It's better than my Portuguese (Hindi, Chinese, Russian...take your pick)."

                        cat fud heer

                        1 Reply Last reply
                        0
                        • S Sentenryu

                          I'm trying to work out how to use the SharpDX toolkit, even the documented areas have really poor descriptions of what they do and what they need... I'm starting to think i should just learn low level DirectX (I plan to, but once i've a high level understanding) Things like that make me think that open source means "Here's my code, you can use it, but you need to fix it first."

                          I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

                          R Offline
                          R Offline
                          RafagaX
                          wrote on last edited by
                          #20

                          I've not used SharpDX, but my understanding is that a very thin wrapper around DirectX, so probably diving in the DirectX documentation can help you (actually DirectX documentation is full of examples that, hopefully, could be easily translated into C# or used directly in most cases).

                          CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                          S 1 Reply Last reply
                          0
                          • M Matthew Faithfull

                            If have a class containing this snippet declared in a C++ header file.

                            protected:
                            
                                \_tChar\* m\_p;
                                
                                //--------------------------------------------------------------------------------
                                struct sHeader
                                {
                                    unsigned short usAlloc;
                                    unsigned short usLen;
                                };
                            
                                //--------------------------------------------------------------------------------
                                struct sFooter
                                {
                                    unsigned short usRefCount;
                                };
                            
                                //--------------------------------------------------------------------------------
                                virtual unsigned short HeaderByteSize( void ) const
                                {
                                    return sizeof( sHeader );
                                }
                            

                            The header is used by half a dozen Dlls which are all part of a project. Instances of the class are created and passed around freely except it doesn't work. Because one dll has decided that sizeof( sHeader ) is 4 bytes as expected and another has decided that it's 16. The same header file, same build options and everything built together in a clean build. :mad::mad: The whole point of using sizeof( sHeader ) in the first place instead of just 4 was so that alignment wouldn't screw thing up. :mad::mad::mad: Now what do I do?

                            "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                            S Offline
                            S Offline
                            Stuart Dootson
                            wrote on last edited by
                            #21

                            To guard against issues like that where the size of objects REALLY mattered, I've always used a static_assert[^] to express static constraints like that:

                            protected:

                                \_tChar\* m\_p;
                                
                                //--------------------------------------------------------------------------------
                                struct sHeader
                                {
                                    unsigned short usAlloc;
                                    unsigned short usLen;
                                };
                            
                                static\_assert(sizeof(sHeader) == 4, "sHeader is an unexpected size!");
                            
                            
                                //--------------------------------------------------------------------------------
                                struct sFooter
                                {
                                    unsigned short usRefCount;
                                };
                            
                                //--------------------------------------------------------------------------------
                                virtual unsigned short HeaderByteSize( void ) const
                                {
                                    return sizeof( sHeader );
                                }
                            

                            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

                            M 1 Reply Last reply
                            0
                            • S Stuart Dootson

                              To guard against issues like that where the size of objects REALLY mattered, I've always used a static_assert[^] to express static constraints like that:

                              protected:

                                  \_tChar\* m\_p;
                                  
                                  //--------------------------------------------------------------------------------
                                  struct sHeader
                                  {
                                      unsigned short usAlloc;
                                      unsigned short usLen;
                                  };
                              
                                  static\_assert(sizeof(sHeader) == 4, "sHeader is an unexpected size!");
                              
                              
                                  //--------------------------------------------------------------------------------
                                  struct sFooter
                                  {
                                      unsigned short usRefCount;
                                  };
                              
                                  //--------------------------------------------------------------------------------
                                  virtual unsigned short HeaderByteSize( void ) const
                                  {
                                      return sizeof( sHeader );
                                  }
                              

                              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

                              M Offline
                              M Offline
                              Matthew Faithfull
                              wrote on last edited by
                              #22

                              A good point, thanks. I have a static_assert and I should be using it.

                              "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                              1 Reply Last reply
                              0
                              • R RafagaX

                                I've not used SharpDX, but my understanding is that a very thin wrapper around DirectX, so probably diving in the DirectX documentation can help you (actually DirectX documentation is full of examples that, hopefully, could be easily translated into C# or used directly in most cases).

                                CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                                S Offline
                                S Offline
                                Sentenryu
                                wrote on last edited by
                                #23

                                the toolkit is a high level abstraction like XNA, the wrapper part is actually well documented with references to the unmanaged documentation and descriptions of the small differences.

                                I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

                                1 Reply Last reply
                                0
                                • M Matthew Faithfull

                                  YvesDaoust wrote:

                                  Could it be a struct member alignment option silently set in a preceding header file

                                  I suspect it might be just that, but leaking from a Windows header. Including pulls in which in this case is a project header not a system header. My guess is that the string support code is being included in the middle of a windows header section that for some reason has different alignment options. Tracking it down or doing anything about it would be rather difficult though. I've worked round this one but I'll be on the lookout next time and also need to get pragma pack support into my Compiler support library as soon as I can.

                                  "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                                  R Offline
                                  R Offline
                                  Ralph Little
                                  wrote on last edited by
                                  #24

                                  Yes, it also occurs to me that your core problem is not the size but the inconsistency. I would look for a platform specific solution to this problem rather than trying to find a platform generic packing instruction. Good luck anyway.

                                  M 1 Reply Last reply
                                  0
                                  • M Matthew Faithfull

                                    If have a class containing this snippet declared in a C++ header file.

                                    protected:
                                    
                                        \_tChar\* m\_p;
                                        
                                        //--------------------------------------------------------------------------------
                                        struct sHeader
                                        {
                                            unsigned short usAlloc;
                                            unsigned short usLen;
                                        };
                                    
                                        //--------------------------------------------------------------------------------
                                        struct sFooter
                                        {
                                            unsigned short usRefCount;
                                        };
                                    
                                        //--------------------------------------------------------------------------------
                                        virtual unsigned short HeaderByteSize( void ) const
                                        {
                                            return sizeof( sHeader );
                                        }
                                    

                                    The header is used by half a dozen Dlls which are all part of a project. Instances of the class are created and passed around freely except it doesn't work. Because one dll has decided that sizeof( sHeader ) is 4 bytes as expected and another has decided that it's 16. The same header file, same build options and everything built together in a clean build. :mad::mad: The whole point of using sizeof( sHeader ) in the first place instead of just 4 was so that alignment wouldn't screw thing up. :mad::mad::mad: Now what do I do?

                                    "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                                    J Offline
                                    J Offline
                                    jschell
                                    wrote on last edited by
                                    #25

                                    Matthew Faithfull wrote:

                                    Now what do I do?

                                    I would start by figuring out what in fact is different in that build. Might also help to investigate what is actually driving the size difference. Presumably packing, or lack thereof, however there is another option.

                                    1 Reply Last reply
                                    0
                                    • R Ralph Little

                                      Yes, it also occurs to me that your core problem is not the size but the inconsistency. I would look for a platform specific solution to this problem rather than trying to find a platform generic packing instruction. Good luck anyway.

                                      M Offline
                                      M Offline
                                      Member 4608898
                                      wrote on last edited by
                                      #26

                                      Is this on a 16-bit, 32-bit or 64 bit environment? If you are using Visual Studio, check the preprocessor directives in all the projects. Also check the code generation struct member alignment. I've had this before: all projects just used default settings except some were VS6, some were VS2003 and some were VS2005. I just got random unexplanable crashes, so, like you, I decided to look at the sizeof things and they were very different. Nowadays I avoid such things by not having inline functions. It may run slower but at least you don't spend weeks trying to figure out stuff like this.

                                      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