473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overhead of C++ struct?

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
7 2738

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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2787
by: f.ruecker | last post by:
Hey Folks, I hope you'll be nice to me and let this post go through even know its mysql and not directly php. But of course my script is php. The problem is, that my Table (1,8 MIO entries/100...
7
3522
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
0
1217
by: Thomas Maier-Komor | last post by:
Hi, let's assume there is an abstract base class (ABC), which is inheritet virtually by two classes (A,B). A and B themselves are inheritet by another class (C). What kind of overhead should I...
19
2264
by: natG | last post by:
On a warehouse app, our Java clients constantly load/insert rows into the db. I would like to throttle these inserts (1.5 million rows per hr) from the Java app, based on current 'busy state' of...
11
3393
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
4
4382
by: DaTurk | last post by:
Hi, I have a c# GUI that needs to untimately send its data down to unmanaged c++. I've decided to do this by having a ref struct in the CLI layer and have the c# populate this, and then pass it...
2
1519
by: mark | last post by:
Hi, I am in the process of developing a small application that sits in the task bar and at a set interval polls an IIS web server for information. The process involves the client sending anything...
2
2599
by: mmcgarry.work | last post by:
Hi, I would like to follow Stroustrup's advice of separating an object interface (abstract class) from an object implementation (concrete class), See Section 15.2.5 in Stroustrup 3rd Edition. ...
36
1995
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.