overhead of C++ struct?

Discussion in 'C++' started by wenmang@yahoo.com, Sep 25, 2006.

  1. Guest

    Hi,
    I have following:
    struct B
    {
    int bINT; //4 bytes
    char bChars[10];
    };
    when I cout the sizeof(B), it turns out to be "16" instead of 14, is
    there a 2 bytes overhead for a C++ struct? How this being interpreted?
    thx
     
    , Sep 25, 2006
    #1
    1. Advertising

  2. Noah Roberts Guest

    wrote:
    > Hi,
    > I have following:
    > struct B
    > {
    > int bINT; //4 bytes
    > char bChars[10];
    > };
    > when I cout the sizeof(B), it turns out to be "16" instead of 14, is
    > there a 2 bytes overhead for a C++ struct? How this being interpreted?


    Probably some form of padding. It is implementation defined.

    Get rid of the 'b' starting your names btw. I don't know who taught
    you that naming scheme but it's terrible.
     
    Noah Roberts, Sep 25, 2006
    #2
    1. Advertising

  3. wrote:
    > Hi,
    > I have following:
    > struct B
    > {
    > int bINT; //4 bytes
    > char bChars[10];
    > };
    > when I cout the sizeof(B), it turns out to be "16" instead of 14, is
    > there a 2 bytes overhead for a C++ struct?


    No.

    > How this being interpreted?


    The compiler is permitted to insert extra bytes into a struct, and
    typically does so for alignment purposes. For example, if your
    platform uses 32-bit words (i.e. sizeof 4, assuming 8-bit chars), it
    would not be unusual for your compiler to pad a struct to make its size
    a multiple of 4. That way, each element of an array of structs is
    guaranteed to start on a word-aligned boundary.

    Presumably that is what is going on here.

    Best regards,

    Tom
     
    Thomas Tutone, Sep 25, 2006
    #3
  4. Guest

    I am working on a project which requires some contexts(A) being stored
    in shared memory. Something like:
    struct B{};
    struct A
    {
    B b[x];
    };

    x is determined dynamically through cmdline option, and I need to
    calculate correct size of B and then A so I can allocate correct memory
    segments before storing them into shm. I am reading other threads in
    this group which mentioned that we should not use sizeof() to determine
    the size of struct, what we are going to use then?

    Thomas Tutone wrote:
    > wrote:
    > > Hi,
    > > I have following:
    > > struct B
    > > {
    > > int bINT; //4 bytes
    > > char bChars[10];
    > > };
    > > when I cout the sizeof(B), it turns out to be "16" instead of 14, is
    > > there a 2 bytes overhead for a C++ struct?

    >
    > No.
    >
    > > How this being interpreted?

    >
    > The compiler is permitted to insert extra bytes into a struct, and
    > typically does so for alignment purposes. For example, if your
    > platform uses 32-bit words (i.e. sizeof 4, assuming 8-bit chars), it
    > would not be unusual for your compiler to pad a struct to make its size
    > a multiple of 4. That way, each element of an array of structs is
    > guaranteed to start on a word-aligned boundary.
    >
    > Presumably that is what is going on here.
    >
    > Best regards,
    >
    > Tom
     
    , Sep 25, 2006
    #4
  5. wrote:

    > I am working on a project which requires some contexts(A) being stored
    > in shared memory. Something like:
    > struct B{};
    > struct A
    > {
    > B b[x];
    > };
    >
    > x is determined dynamically through cmdline option, and I need to
    > calculate correct size of B and then A so I can allocate correct memory
    > segments before storing them into shm. I am reading other threads in
    > this group which mentioned that we should not use sizeof() to determine
    > the size of struct


    Please provide a link to those threads.

    > what we are going to use then?


    sizeof is, as far as I know, the correct way to determine the size of a
    struct.

    Best regards,

    Tom
     
    Thomas Tutone, Sep 25, 2006
    #5
  6. wrote:

    > I am working on a project which requires some contexts(A) being stored
    > in shared memory. Something like:
    > struct B{};
    > struct A
    > {
    > B b[x];
    > };
    >
    > x is determined dynamically through cmdline option, and I need to
    > calculate correct size of B and then A so I can allocate correct memory


    If you use a compiler that allows that dynamically sized struct, it will
    probably have a sizeof, or other implementation specific mean, that can do
    the job.

    A better C++ way to do that can be to use std::vector with a custom
    allocator that allocates in the shared memory you use.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Sep 25, 2006
    #6
  7. Guest

    Wondering whether you can provide code sample or link to some websites
    that use concept of customized allocator of vector for storing objects
    in shm.
    The other is that after parsing cmdline, the struct of context will be
    fixed and all contexts will have same size and internal struct. Thx.


    Julián Albo wrote:
    > wrote:
    >
    > > I am working on a project which requires some contexts(A) being stored
    > > in shared memory. Something like:
    > > struct B{};
    > > struct A
    > > {
    > > B b[x];
    > > };
    > >
    > > x is determined dynamically through cmdline option, and I need to
    > > calculate correct size of B and then A so I can allocate correct memory

    >
    > If you use a compiler that allows that dynamically sized struct, it will
    > probably have a sizeof, or other implementation specific mean, that can do
    > the job.
    >
    > A better C++ way to do that can be to use std::vector with a custom
    > allocator that allocates in the shared memory you use.
    >
    > --
    > Salu2
     
    , Sep 25, 2006
    #7
  8. posted:

    > struct B
    > {
    > int bINT; //4 bytes



    It's 4 bytes on a lot of systems, but this is not a requirement of the
    Standard.


    > char bChars[10];
    > };
    > when I cout the sizeof(B), it turns out to be "16" instead of 14, is
    > there a 2 bytes overhead for a C++ struct? How this being interpreted?



    It's padding to satisfy the alignment requirements of the machine. (Not
    necessarily "requirements", perhaps just a more efficient setup.)

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 25, 2006
    #8
    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. John T. Goodman

    Overhead of 4-port over 2-port SRAM

    John T. Goodman, Jan 25, 2005, in forum: VHDL
    Replies:
    0
    Views:
    659
    John T. Goodman
    Jan 25, 2005
  2. Showjumper

    Does Option Strict On add overhead?

    Showjumper, Jul 31, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    370
    John Saunders
    Jul 31, 2003
  3. Brett Robichaud

    Trace overhead

    Brett Robichaud, May 5, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    523
    Brett Robichaud
    May 6, 2004
  4. Dave Williamson
    Replies:
    2
    Views:
    465
    Rocky Moore
    Aug 15, 2004
  5. Chris Fogelklou
    Replies:
    36
    Views:
    1,441
    Chris Fogelklou
    Apr 20, 2004
Loading...

Share This Page