overhead of C++ struct?

W

wenmang

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
 
N

Noah Roberts

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.
 
T

Thomas Tutone

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
 
W

wenmang

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 said:
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
 
T

Thomas Tutone

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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

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.
 
W

wenmang

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 said:
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.
 
F

Frederick Gotham

(e-mail address removed) 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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top