Struct size vs. Class size in memory

Discussion in 'C++' started by Jaco Naude, Jan 13, 2010.

  1. Jaco Naude

    Jaco Naude Guest

    Hi

    I am working with big matrices filled with complex values. I
    considered to use the std complex.h class to represent the complex
    numbers, but in the end I wrote my own small struct to store the
    complex numbers. I created a separate header file with all the
    functions related to complex numbers which operate on complexValue
    structs.

    This struct is below:

    struct complexValue {
    complexValue(qreal i = 0, qreal q = 0) :
    real(i),
    imag(q) { }
    //! Copy constructor.
    complexValue(const complexValue& other) {
    real = other.real;
    imag = other.imag;
    }
    qreal real;
    qreal imag;
    };

    My reasoning behind this was that big matrices using my struct will be
    much smaller than big matrices with a std::complex object instance for
    each value. My reasoning behind the seperate header file with the
    complex math operations is that the memory needed for each math
    operation will not be included in each object instance.

    I tried to verify this using the following:

    complex<double> testStd(0.1,0.1);
    complexValue testOwn(0.1,0.1);
    int stdSize = sizeof(testStd);
    int ownSize = sizeof(testOwn);

    The result is that stdSize = ownSize = 16.

    Am I missing something or is the compiler too clever for me?

    Thanks in advance,
    Jaco
    Jaco Naude, Jan 13, 2010
    #1
    1. Advertising

  2. Jaco Naude

    Ian Collins Guest

    Jaco Naude wrote:

    <snip>

    > My reasoning behind this was that big matrices using my struct will be
    > much smaller than big matrices with a std::complex object instance for
    > each value. My reasoning behind the seperate header file with the
    > complex math operations is that the memory needed for each math
    > operation will not be included in each object instance.


    What memory? the size of an instance of a class or struct without
    virtual methods is the size of its data members plus any padding
    required for alignment.

    > I tried to verify this using the following:
    >
    > complex<double> testStd(0.1,0.1);
    > complexValue testOwn(0.1,0.1);
    > int stdSize = sizeof(testStd);
    > int ownSize = sizeof(testOwn);
    >
    > The result is that stdSize = ownSize = 16.
    >
    > Am I missing something or is the compiler too clever for me?


    I think you were assuming the code space for class methods was part of
    the size of an instance of the class.

    --
    Ian Collins
    Ian Collins, Jan 13, 2010
    #2
    1. Advertising

  3. Jaco Naude

    Jaco Naude Guest

    On Jan 13, 12:54 pm, Ian Collins <> wrote:
    > Jaco Naude wrote:
    >
    > <snip>
    >
    > > My reasoning behind this was that big matrices using my struct will be
    > > much smaller than big matrices with a std::complex object instance for
    > > each value. My reasoning behind the seperate header file with the
    > > complex math operations is that the memory needed for each math
    > > operation will not be included in each object instance.

    >
    > What memory?  the size of an instance of a class or struct without
    > virtual methods is the size of its data members plus any padding
    > required for alignment.
    >
    > > I tried to verify this using the following:

    >
    > >     complex<double> testStd(0.1,0.1);
    > >     complexValue testOwn(0.1,0.1);
    > >     int stdSize = sizeof(testStd);
    > >     int ownSize = sizeof(testOwn);

    >
    > > The result is that stdSize = ownSize = 16.

    >
    > > Am I missing something or is the compiler too clever for me?

    >
    > I think you were assuming the code space for class methods was part of
    > the size of an instance of the class.
    >
    > --
    > Ian Collins


    You are correct, I was not sure how this works. Thanks for the
    explanation...
    Cheers
    Jaco
    Jaco Naude, Jan 13, 2010
    #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. E11
    Replies:
    1
    Views:
    4,719
    Thomas Weidenfeller
    Oct 12, 2005
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,357
    Chris Fogelklou
    Apr 20, 2004
  3. Replies:
    5
    Views:
    520
    Flash Gordon
    Apr 9, 2006
  4. Replies:
    8
    Views:
    488
    Bob Hairgrove
    Apr 10, 2006
  5. Replies:
    2
    Views:
    727
    David Harmon
    Sep 20, 2006
Loading...

Share This Page