Re: memory alignment?

Discussion in 'C++' started by Peter van Merkerk, Aug 1, 2003.

  1. > If I have a class like below.
    > class test {
    > int a;
    > double b;
    > };
    >
    > Using sizeof(test), it'll get 16 bytes.
    > Because I maybe handle larger than 1 million such kind objects.
    > Can't we reduce this class to only cost 12 bytes?


    This is a compiler specific question, there is no standard way to
    accomplish what you want. You can probably reduce the size of the class
    with a compiler setting or a #pragma (on some compilers #pragma pack
    does the trick).

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Aug 1, 2003
    #1
    1. Advertising

  2. "Peter van Merkerk" <> wrote in message news:<bgdbko$ngf9o$-berlin.de>...
    > > If I have a class like below.
    > > class test {
    > > int a;
    > > double b;
    > > };
    > >
    > > Using sizeof(test), it'll get 16 bytes.
    > > Because I maybe handle larger than 1 million such kind objects.
    > > Can't we reduce this class to only cost 12 bytes?

    >
    > This is a compiler specific question, there is no standard way to
    > accomplish what you want. You can probably reduce the size of the class
    > with a compiler setting or a #pragma (on some compilers #pragma pack
    > does the trick).


    A simple reorganization might do the trick:

    class test {
    double b;
    int a;
    };

    If that doesn't work, you'll have to use Peter's advice and look
    through your compiler documentation for the pack pragma, which still
    might not work.

    Alignment is a tricky issue and bad alignment will often cause
    crashes. Unaligned accesses will almost always cause a performance
    hit.

    In general, I'd trust the compiler and accept that your 1 million
    objects will each take 16 bytes. Pre-optimization is rarely good. IMO,
    only as a last resort should you start worrying about packing your
    objects.

    Dave
     
    David Cattarin, Aug 1, 2003
    #2
    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. Thomas Matthews

    Re: memory alignment?

    Thomas Matthews, Aug 1, 2003, in forum: C++
    Replies:
    0
    Views:
    374
    Thomas Matthews
    Aug 1, 2003
  2. Andrew Koenig

    Re: memory alignment?

    Andrew Koenig, Aug 1, 2003, in forum: C++
    Replies:
    1
    Views:
    333
    Karl Heinz Buchegger
    Aug 1, 2003
  3. J. Campbell
    Replies:
    14
    Views:
    511
    Default User
    Oct 9, 2003
  4. Paul_Huang

    Memory Padding and alignment

    Paul_Huang, Sep 21, 2004, in forum: C++
    Replies:
    2
    Views:
    3,980
    Serge Paccalin
    Sep 21, 2004
  5. aneesh

    memory alignment

    aneesh, Sep 24, 2003, in forum: C Programming
    Replies:
    3
    Views:
    1,272
    Micah Cowan
    Sep 24, 2003
Loading...

Share This Page