CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2003
    Posts
    424

    #pragma pack is useless!!!

    I'm using VC++ 2002 and I cannot set the packing alignment of a class!

    #pragma pack(push)
    #pragma pack(1)
    #pragma pack(show) //shows ==1 in warnings

    class...

    #pragma pack(pop)

    When I run my program, sizeof(myClass) shows 52 rather than 46 all the time, unless I put #pragma pack(1) at the beginning of my code without pushing or popping but then all my code is one byte aligned!

  2. #2
    Join Date
    May 2003
    Posts
    424

    Re: #pragma pack is useless!!!

    Just read that pack has no effect on definitions. I never use declarations so I have another question...

    Is it possible that VC++ could built bad code with 1 byte alignment specified? Because I have located exactly where a runtime error takes place and it cannot be my fault.

    And to prove that further; the error never takes place with 4 byte alignment and the runtime error has nothing to do with writing the class to disk or anything like it.

  3. #3
    Join Date
    May 2003
    Posts
    424

    Re: #pragma pack is useless!!!

    Just tried breaking class down to a declaration and putting the #pragma's only around that; still no effect!!!! Now I'm angry!

    So I'm assuming that it SHOULD work the way I use classes and the breaking classes apart into separate declarations and defs.

    For example:
    My way:
    #pragma pack(1)
    class a
    {
    char c;
    void Funk(int x){return;}
    };

    Separated way:
    #pragma pack(1)
    class a
    {
    char c;
    void Funk(int x);
    };

    void a::Funk(int x){return;}

  4. #4
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645

    Re: #pragma pack is useless!!!

    You haven't given any information on the structure of you class.

    What is the structure of the class that you are wanting to pack?

    Show your code including the debug messages. Show the debug
    output also.

  5. #5
    Join Date
    May 2003
    Posts
    424

    Re: #pragma pack is useless!!!

    PHP Code:
    #pragma pack(push)
    #pragma pack(1)
    #pragma pack(show)

    class Setts
    {
    public:
        
    aArr<WBmp*> wbmpArr;
        
    float defTime;
        
    byte def_cent_Stre_Tile;
        
    COLORREF defBackCol;
        
    bool pause;
        
    bool startupOnly;
        
    aStr XDir;
        
    ushort curInd;
        
    aSize resizeSize;
        
    bool keepProportion;
        
    bool runOnStartup;
        
    bool autoBackColSel;

        
    Setts()
        {
            
    defTime30.0f;
            
    def_cent_Stre_Tile0;
            
    defBackColRGB(122,150,223);
            
    pausestartupOnly0;
            
    App::ExecutingDir(XDir);
            
    XDirXDir<<"\\X_DATA";
            
    curInd0;
            
    aRect rScreen::WorkingArea(r);
            
    aSize scrSScreen::Size();
            
    resizeSize.set(r.w-((scrS.w-r.w)*2), r.h-((scrS.h-r.h)*2));
            
    keepProportion1;
            
    runOnStartup1;
            
    autoBackColSel1;
        }
        ~
    Setts()
        {
            for(
    int i0i<wbmpArr.Count; ++i)
            {
                
    delete wbmpArr[i];
            }
        }
    };

    #pragma pack(pop)
    #pragma pack(show) 
    Compiler list:

    d:\Files\C++ projects\a\aGraphics.h(988): warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
    d:\Files\C++ projects\a\a1.h(1038): warning C4244: 'initializing' : conversion from 'double' to 'uint', possible loss of data
    d:\Files\C++ projects\a\aButton.h(104): warning C4244: 'initializing' : conversion from 'float' to 'int', possible loss of data
    d:\Files\C++ projects\a\aButton.h(112): warning C4244: 'initializing' : conversion from 'float' to 'int', possible loss of data
    d:\Files\C++ projects\a\aButton.h(118): warning C4244: 'initializing' : conversion from 'float' to 'int', possible loss of data
    d:\Files\C++ projects\a\aButton.h(119): warning C4244: 'initializing' : conversion from 'float' to 'int', possible loss of data
    d:\Files\C++ projects\a\aScrollBar.h(92): warning C4244: 'initializing' : conversion from 'float' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(5378): warning C4244: 'initializing' : conversion from 'float' to 'ulong', possible loss of data
    d:\Files\C++ projects\a\a1.h(2299): warning C4244: 'return' : conversion from 'DWORD' to 'byte', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(107): warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(315): warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(505): warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\a1.h(245): warning C4267: '=' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(700): warning C4267: '=' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(734): warning C4267: '=' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(71): warning C4267: '=' : conversion from 'size_t' to 'ushort', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(82): warning C4267: '=' : conversion from 'size_t' to 'ushort', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(376): warning C4267: '=' : conversion from 'size_t' to 'ushort', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(383): warning C4267: '=' : conversion from 'size_t' to 'ushort', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(440): warning C4267: '=' : conversion from 'size_t' to 'ushort', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(462): warning C4267: '=' : conversion from 'size_t' to 'ushort', possible loss of data
    d:\Files\C++ projects\a\a1.h(116): warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\a1.h(123): warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(563): warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\a1.h(244): warning C4267: 'argument' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(1303): warning C4267: 'argument' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(6113): warning C4267: 'argument' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(6205): warning C4267: 'argument' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\aFileDirList.h(57): warning C4267: 'argument' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(424): warning C4267: 'initializing' : conversion from 'size_t' to 'byte', possible loss of data
    d:\Files\C++ projects\a\a1.h(1408): warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\a1.h(1418): warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\a1.h(1428): warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\a1.h(3357): warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\aEditBox.h(112): warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\aFileDirList.h(137): warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
    d:\Files\C++ projects\a\a1.h(236): warning C4267: 'initializing' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(324): warning C4267: 'initializing' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(349): warning C4267: 'initializing' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(371): warning C4267: 'initializing' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(639): warning C4267: 'initializing' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(776): warning C4267: 'initializing' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\a1.h(2354): warning C4267: 'initializing' : conversion from 'size_t' to 'uint', possible loss of data
    d:\Files\C++ projects\a\aColPicker.h(315): warning C4311: 'type cast' : pointer truncation from 'Col *' to 'int'
    d:\Files\C++ projects\a\a1.h(5179): warning C4312: 'type cast' : conversion from 'int' to 'const cchar *' of greater size
    d:\Files\C++ projects\a\a1.h(5185): warning C4312: 'type cast' : conversion from 'int' to 'const cchar *' of greater size
    d:\Files\C++ projects\a\a1.h(5763): warning C4312: 'type cast' : conversion from 'int' to 'const cchar *' of greater size
    d:\Files\C++ projects\a\aControlBase.h(452): warning C4312: 'type cast' : conversion from 'LONG' to 'aControlBase *' of greater size
    d:\files\c++ projects\a\acontrolbase.h(18): warning C4715: 'aControlIDs::getID' : not all control paths return a value
    d:\Files\C++ projects\a\a1.h(1812): warning C4800: '_iobuf *' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\a1.h(2730): warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\a1.h(2737): warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aControlBase.h(273): warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aControl.h(474): warning C4800: 'byte' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aControl.h(482): warning C4800: 'byte' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aForm.h(233): warning C4800: 'byte' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aForm.h(241): warning C4800: 'byte' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aForm.h(260): warning C4800: 'byte' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aForm.h(271): warning C4800: 'byte' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aForm.h(275): warning C4800: 'byte' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aListBox.h(448): warning C4800: 'byte' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aListBox.h(458): warning C4800: 'byte' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\a1.h(1957): warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\a1.h(2063): warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\a\aControl.h(831): warning C4800: 'int_ptr' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\Wallpaper Rotator X\Main.cpp(421): warning C4800: 'int_ptr' : forcing value to bool 'true' or 'false' (performance warning)
    d:\Files\C++ projects\Wallpaper Rotator X\Options.h(3): warning C4810: value of pragma pack(show) == 1
    d:\Files\C++ projects\Wallpaper Rotator X\Options.h(155): warning C4810: value of pragma pack(show) == 4
    Wallpaper Rotator X warning LNK4235: /LTCG specified but no code generation required; remove /LTCG from the link command line to improve linker performance

  6. #6
    Join Date
    May 2003
    Posts
    424

    Re: #pragma pack is useless!!!

    Here's the scoop. I did not take into account aStr and aArr's 4 byte aligned sizes which added 3 bytes each making 46, 52.

    I am happy to say the #pragma's are working properly! However, I am still confused as to why Visual Studio caused runtime errors in my program because of 1 byte alignment for everything.

  7. #7
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: #pragma pack is useless!!!

    I doubt that 1-byte alignment causes error all by itself. A possible reason is that your may have overwritten the allocated memory causing the next variable or instance to be corrupted. For example, it is very common to loop an extra time.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: #pragma pack is useless!!!

    Quote Originally Posted by aewarnick
    [php]#pragma pack(push)
    Wait a second....You're class is non-POD. You can't just "add up" the bytes and come up with a specific number yourself. Only POD classes can be utilized this way.

    Your class consists of a user-defined destructor. Once you do this, the layout is implementation defined. It may be 46 (or 52) for this version of VC++, but for another version, it may be 50, or 54, or 58 or... Again, you just can't add up the bytes by hand and come out with a definite answer if the class/struct is non-POD. POD yes, non-POD, no.

    Also, if you're going to post a class, also post what those unknown types are such as "aArr". How do you know the size of this member variable? What exactly is it? Is it also a non-POD template type?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 24th, 2005 at 08:59 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured