473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

I'm confused about the space a class takes in a structure?

Pep
I'm getting weird results at the moment so thought I'd rebase my
knowledge of c++ storage with your help :)

I have a class used as a type in a struct and the struct is handled by
a 3rd party binary writed for persistent storage. I am confused by the
results I am getting and am not sure how the 3rd party writer is
seeing the size of the stuct.

class foo
// I have not included the methods for brevity
{
public:
unsigned short shortArray[4];
};

typedef struct
{
foo fooVar;
char charArray[8];
} bar;

So if you do a sizeof(foo) you get back 8 as a result and a
sizeof(bar) returns 16. However the class foo has methods as well. Now
if I was to do any form of binary copy on bar such as using memcpy for
example using the sizeof(bar) as the length, will I definitely be
copying the complete data in bar or will the operation be thrown out
by things like internal members in foo created by the compiler, like
the vtbl in foo?

TIA :)

Aug 17 '07 #1
7 1490
Pep wrote:
I'm getting weird results at the moment so thought I'd rebase my
knowledge of c++ storage with your help :)

I have a class used as a type in a struct and the struct is handled by
a 3rd party binary writed for persistent storage. I am confused by the
results I am getting and am not sure how the 3rd party writer is
seeing the size of the stuct.

class foo
// I have not included the methods for brevity
{
public:
unsigned short shortArray[4];
};
It makes a difference what kind of "methods" this class has. If it
doesn't have user-defined constructors, or virtual functions, it's
most likely a POD class, which means it's safe to use 'memcpy' on it.
typedef struct
{
foo fooVar;
char charArray[8];
} bar;
Tell us, why do you do the wicked typedef dance here? Why don't you
simply write

struct bar
{
foo fooVar;
char charArray[8];
};

?
>
So if you do a sizeof(foo) you get back 8 as a result
OK, so your 'short' is 2 bytes long, most likely.
and a
sizeof(bar) returns 16.
It seems that the compiler adds no padding in the 'bar' objects.
However the class foo has methods as well.
Again, depends on what methods those are.
Now
if I was to do any form of binary copy on bar such as using memcpy for
example using the sizeof(bar) as the length, will I definitely be
copying the complete data in bar or will the operation be thrown out
by things like internal members in foo created by the compiler, like
the vtbl in foo?
If sizeof(foo) == sizeof(short[4]), there are no "internal members"
with which you need to concern yourself.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 17 '07 #2
Pep wrote:
So if you do a sizeof(foo) you get back 8 as a result and a
sizeof(bar) returns 16. However the class foo has methods as well. Now
if I was to do any form of binary copy on bar such as using memcpy for
example using the sizeof(bar) as the length, will I definitely be
copying the complete data in bar or will the operation be thrown out
by things like internal members in foo created by the compiler, like
the vtbl in foo?
How can foo have a virtual table when it has no virtual methods?
If it had a virtual table you would see it in the size of foo:
Instead of being 8 (ie. 4 2-byte shorts) it would probably be
12 (or 16 if you are compiling in a 64-bit system). That's because
a class with virtual methods has (in most compiler implementations)
a pointer to the virtual table inside it.

Even if it had virtual methods, it doesn't really matter. The pointer
to the virtual table will be the same for all objects of the same type.
Thus if you copy an object of type foo to an object of type foo, the
virtual table pointer will be the same.

Exactly what kind of incomplete data would you expect memcpy() to copy
in this case?
Of course you should take into account that memcpy() skips
constructors, copy constructors and assignment operators. Assuming you
don't have any of those, I suppose it doesn't matter.
Aug 17 '07 #3
Pep

Juha Nieminen wrote:
Pep wrote:
So if you do a sizeof(foo) you get back 8 as a result and a
sizeof(bar) returns 16. However the class foo has methods as well. Now
if I was to do any form of binary copy on bar such as using memcpy for
example using the sizeof(bar) as the length, will I definitely be
copying the complete data in bar or will the operation be thrown out
by things like internal members in foo created by the compiler, like
the vtbl in foo?

How can foo have a virtual table when it has no virtual methods?
If it had a virtual table you would see it in the size of foo:
Instead of being 8 (ie. 4 2-byte shorts) it would probably be
12 (or 16 if you are compiling in a 64-bit system). That's because
a class with virtual methods has (in most compiler implementations)
a pointer to the virtual table inside it.
Yep, it was more a badly worded question as to "what should I look out
for" to refresh my memory as usually I no longer have to worry about
the sort of things any more.
Even if it had virtual methods, it doesn't really matter. The pointer
to the virtual table will be the same for all objects of the same type.
Thus if you copy an object of type foo to an object of type foo, the
virtual table pointer will be the same.

Exactly what kind of incomplete data would you expect memcpy() to copy
in this case?
Of course you should take into account that memcpy() skips
constructors, copy constructors and assignment operators. Assuming you
don't have any of those, I suppose it doesn't matter.
Yep, I don;t really know if they are using memcpy in the database code
or not as they are very cagey when we talk to them about their
product. All I do know is that they are definitely doing binary copy
(of some form) on the data we send which they expect to be a struct
type.

So basically from what I can glean from everyones answers is that I
can rely on the result of the sizeof() off the struct against my
knowledge of the sizeof the sum of the scalars in the struct and the
class. As long as those are the same then I should have no problems.

Cheers.

Aug 17 '07 #4
On Aug 17, 8:33 am, Pep <pepaltavi...@yahoo.co.ukwrote:
Juha Nieminen wrote:
Pep wrote:
So if you do a sizeof(foo) you get back 8 as a result and a
sizeof(bar) returns 16. However the class foo has methods as well. Now
if I was to do any form of binary copy on bar such as using memcpy for
example using the sizeof(bar) as the length, will I definitely be
copying the complete data in bar or will the operation be thrown out
by things like internal members in foo created by the compiler, like
the vtbl in foo?
How can foo have a virtual table when it has no virtual methods?
If it had a virtual table you would see it in the size of foo:
Instead of being 8 (ie. 4 2-byte shorts) it would probably be
12 (or 16 if you are compiling in a 64-bit system). That's because
a class with virtual methods has (in most compiler implementations)
a pointer to the virtual table inside it.

Yep, it was more a badly worded question as to "what should I look out
for" to refresh my memory as usually I no longer have to worry about
the sort of things any more.
Even if it had virtual methods, it doesn't really matter. The pointer
to the virtual table will be the same for all objects of the same type.
Thus if you copy an object of type foo to an object of type foo, the
virtual table pointer will be the same.
Exactly what kind of incomplete data would you expect memcpy() to copy
in this case?
Of course you should take into account that memcpy() skips
constructors, copy constructors and assignment operators. Assuming you
don't have any of those, I suppose it doesn't matter.

Yep, I don;t really know if they are using memcpy in the database code
or not as they are very cagey when we talk to them about their
product. All I do know is that they are definitely doing binary copy
(of some form) on the data we send which they expect to be a struct
type.
In that case, your best bet may be to create a POD type that mirrors
your class, copy into the POD, and forward to the database class for
serialization.

Alternatively, if you can rewrite some of your code....

struct PODdata
{
// all the POD data to be serialized goes here
};

class Myclass : private PODdata // note private inheritance
{
// methods, constructors, etc....
public:
PODdata* as_PODdata() { return this; }
};

And pass the result of as_PODdata() to your serializer.
Aug 17 '07 #5
>
So basically from what I can glean from everyones answers is that I
can rely on the result of the sizeof() off the struct against my
knowledge of the sizeof the sum of the scalars in the struct and the
class. As long as those are the same then I should have no problems.

Cheers.
size of the struct may be greater than the sum of the sizes of its
members due to alignment issues unless your compiler align them at
byte
boundaries which it doesn't if you haven't specifically said so.

Aug 17 '07 #6
hurcan solter wrote:
size of the struct may be greater than the sum of the sizes of its
members due to alignment issues unless your compiler align them at
byte
boundaries which it doesn't if you haven't specifically said so.
Note that one cannot assume that elements in structs/classes can be
packed at byte boundaries in all architectures. There exist
architectures (such as the Sun UltraSparc) where it's just not possible
to store multiple-byte elements at anything else than at word bounaries
(trying to access a multiple-byte element not at word boundary will
cause a bus error interrupt).
Aug 18 '07 #7

"Juha Nieminen" <no****@thanks.invalidwrote in message
news:46**********************@news.song.fi...
hurcan solter wrote:
> size of the struct may be greater than the sum of the sizes of its
members due to alignment issues unless your compiler align them at
byte
boundaries which it doesn't if you haven't specifically said so.

Note that one cannot assume that elements in structs/classes can be
packed at byte boundaries in all architectures.
Which may not be a problem if the program runs on the platform without
communicating to another platform: "fix it up" at he boundary to match up
with the characteristics of the platform that was chosen to represent the
protocol.

John

Aug 18 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: jfj | last post by:
I don't understand. We can take a function and attach it to an object, and then call it as an instance method as long as it has at least one argument: ############# class A: pass def...
0
by: Mike | last post by:
Hi, I am consuming a web service in my asp.net application. I added the web references in Visual Studio by pointing to a wsdl file. Visual Studio automatically creates the Reference.cs file and...
6
by: DH | last post by:
I have a VERY basic question about figuring database size. I've inherited a database which is generally similar to this basic one: Item, Red, Blue, Green, Yellow (text), (int),(int),(int),(int)...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
3
by: Bopolissimus Platypus Jr | last post by:
hello all, i've got a database that takes up 4G of space. when i run a script that deletes all rows and then vacuum, the data directory gets down to around 3-3.5G. what i'd like is to get a...
3
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
10
by: Ruan | last post by:
My confusion comes from the following piece of code: memo = {1:1, 2:1} def fib_memo(n): global memo if not n in memo: memo = fib_memo(n-1) + fib_memo(n-2) return memo I used to think that...
10
by: Phil Stanton | last post by:
I have a table of SpaceAreas eg Food Store, Garden Shed etc with the first and last bin for each Space Area defined. eg Food Store First Space 1, last space 26 and Gargen Shed First space 1, last...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.