By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

overhead of C++ struct?

P: n/a
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 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

we*****@yahoo.com 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.

Sep 25 '06 #2

P: n/a

we*****@yahoo.com 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 '06 #3

P: n/a
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:
we*****@yahoo.com 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 '06 #4

P: n/a
we*****@yahoo.com 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

Sep 25 '06 #5

P: n/a
we*****@yahoo.com 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 '06 #6

P: n/a
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:
we*****@yahoo.com 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 '06 #7

P: n/a
we*****@yahoo.com 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
Sep 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.