sizeof of an instance differ from sizeof of a class

Discussion in 'C++' started by GRenard, Nov 2, 2006.

  1. GRenard

    GRenard Guest

    Hello,

    We just switch in our company to VisualStudio 2005 and the new ATL
    library.
    We use a wrapper to use CFileDialog. Its name is CFileDialogDeluxe

    Here the call
    CFileDialogDeluxe oFileDialog( ... );

    There are some problems in the debug, look the sizeof from the debug :

    sizeof(oFileDialog) 1144 unsigned int
    sizeof(CFileDialogDeluxe) 1156 unsigned int
    sizeof((*(WTL::CFileDialog*)(&oFileDialog))) 1156 unsigned int

    Now the CFileDialogDeluxe just call the constructor of CFileDialog, I
    removed all the members from our class.
    But when I put members, there are some offsets on the address which
    make the program crashes when destructing variables.

    So what can affect the sizeof of a variable like that because it
    inserts offset of variables.


    Thank you very much

    Jean-S├ębastien Goupil
     
    GRenard, Nov 2, 2006
    #1
    1. Advertising

  2. GRenard

    David W Guest

    Possibly you are compiling different files with different structure/class member packing. For some
    compilers, members can be aligned to reduce memory accesses by the CPU or they can be packed
    together as tightly as possible, depending on compiler options chosen. If the options differ between
    source files, you'll get a mismatch in member location between different parts of your program. I
    have had programs crash for this reason. Look up your compiler's documemtation for member packing
    options..

    DW
     
    David W, Nov 3, 2006
    #2
    1. Advertising

  3. GRenard

    Salt_Peter Guest

    GRenard wrote:
    > Hello,
    >
    > We just switch in our company to VisualStudio 2005 and the new ATL
    > library.
    > We use a wrapper to use CFileDialog. Its name is CFileDialogDeluxe
    >
    > Here the call
    > CFileDialogDeluxe oFileDialog( ... );
    >
    > There are some problems in the debug, look the sizeof from the debug :
    >
    > sizeof(oFileDialog) 1144 unsigned int
    > sizeof(CFileDialogDeluxe) 1156 unsigned int
    > sizeof((*(WTL::CFileDialog*)(&oFileDialog))) 1156 unsigned int
    >
    > Now the CFileDialogDeluxe just call the constructor of CFileDialog, I
    > removed all the members from our class.
    > But when I put members, there are some offsets on the address which
    > make the program crashes when destructing variables.
    >
    > So what can affect the sizeof of a variable like that because it
    > inserts offset of variables.
    >
    >
    > Thank you very much
    >
    > Jean-S├ębastien Goupil


    Padding is causing the offsets to change. Padding should never affect
    the destruction of members. If it does, you've got undefined behaviour.
    If you are allocating/deallocating members through pointers based on
    the sizeof involved and their "offsets", you have undefined behaviour.
    That is strictly forbidden in C++. Padding can change depending on
    platform and compiler switches. A properly designed program remains
    unaffected by these options.

    Lets take a silly example:
    #include <iostream>

    struct A
    {
    int n;
    char c;
    };

    int main()
    {
    A a;
    std::cout << "sizeof(int) = " << sizeof(int) << std::endl;
    std::cout << "sizeof(char) = " << sizeof(char) << std::endl;
    std::cout << "sizeof(A) = " << sizeof(A) << std::endl;
    }

    /*
    sizeof(int) = 4
    sizeof(char) = 1
    sizeof(A) = 8
    */

    This is expected. The compiler on this particular platform padded the
    class to match this computer's memory architecture.
    If i had newed/malloc the instances of the class and/or members, its my
    responsability to deallocate those objects and/or members via pointers
    that are unaffected by the padding involved. Thats the law.
     
    Salt_Peter, Nov 3, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sridhar R
    Replies:
    14
    Views:
    1,409
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Steven T. Hatton
    Replies:
    9
    Views:
    488
  3. Martin P. Hellwig
    Replies:
    1
    Views:
    377
    Martin P. Hellwig
    Mar 26, 2010
  4. Nikolai Weibull

    sizeof(Class)/sizeof(Object)

    Nikolai Weibull, Dec 31, 2004, in forum: Ruby
    Replies:
    2
    Views:
    149
    Robert Klemme
    Dec 31, 2004
  5. kunjaan
    Replies:
    8
    Views:
    114
    Charles Oliver Nutter
    May 28, 2009
Loading...

Share This Page