Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Difference between struct and class and pointer to member data

Reply
Thread Tools

Difference between struct and class and pointer to member data

 
 
Pep
Guest
Posts: n/a
 
      09-09-2009
Think it is about time I got this straight in my head, with the help
of you guys of course

So a struct is a collection of data members as is a class, where a
class default scope is private and a struct default scope is public.
All well and good so far.

If I send the address of a struct or class to a function within my
program everything is well and good, obviously all of the class/
struct, i.e. the members, methods and vtable exist in my program.

Now here is the point I get a little fuzzy, what happens if I want to
serialize the struct/class. Assuming I send the address of the struct/
class and it's size() as parameters to my serialize function, I can
effectively write the size() number of bytes from the address
parameter to a file. What happens with the methods and vtable of a
struct/class when you serialize it?

I know that I can easily allocate a region of memory and cast it to a
struct/class and effectively use it as an instantiated instance of the
struct/class. So I suspect the obvious answer is that the methods and
vtable are discarded and reapplied when the memory region when cast
against a struct/class.

Presumably, the only way a struct/class can be deserialized is by a
function that knows the declaration of the struct/class?

So is what I outlined correct in terms of the c++ standard or only for
certain c++ compilers that may have extended things?
 
Reply With Quote
 
 
 
 
Michael Tsang
Guest
Posts: n/a
 
      09-09-2009
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pep wrote:

> Think it is about time I got this straight in my head, with the help
> of you guys of course
>
> So a struct is a collection of data members as is a class, where a
> class default scope is private and a struct default scope is public.
> All well and good so far.
>
> If I send the address of a struct or class to a function within my
> program everything is well and good, obviously all of the class/
> struct, i.e. the members, methods and vtable exist in my program.
>
> Now here is the point I get a little fuzzy, what happens if I want to
> serialize the struct/class. Assuming I send the address of the struct/
> class and it's size() as parameters to my serialize function, I can
> effectively write the size() number of bytes from the address
> parameter to a file. What happens with the methods and vtable of a
> struct/class when you serialize it?
>
> I know that I can easily allocate a region of memory and cast it to a
> struct/class and effectively use it as an instantiated instance of the
> struct/class. So I suspect the obvious answer is that the methods and
> vtable are discarded and reapplied when the memory region when cast
> against a struct/class.
>
> Presumably, the only way a struct/class can be deserialized is by a
> function that knows the declaration of the struct/class?
>
> So is what I outlined correct in terms of the c++ standard or only for
> certain c++ compilers that may have extended things?


Difference between class and struct:
class has private access level, struct has public access level;
class can be used to declare template parameter, struct can't.

When you serialize (dump) a class into a file, the content will be the
internal representation of a class (including any v-table, padding bytes,
etc.). Therefore, dumping any class with pointers (including all classes
with virtual members and inheritance, as they have a hidden pointer at the
front) is useless. Deserializing a class (providing it does not have any
pointers) is similar to serializing a class, you only need to pass the
pointer and the size. Therefore, to deserialize a class, you just need to
call fread(3).

Here is the criteria for classes which are safe to serialize:

- - It does not contain any pointers/references.
- - It does not contain any virtual functions, including virtual functions
inherited by base classes.
- - It does not have virtual base classes, including virtual base classes
indirectly inherited by base classes.
- - It does not contain any classes which aren't safe to serialize.

To deserialize, the deserializing program and the serializing program must
be ABI-compatible in order to obtain useful results.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkqntZcACgkQG6NzcAXitM9EwgCdGrEe+FgSYN IxOMEA+2bO+Uqe
sFcAnjgD8glOMGKSu2+4IO+lYEBaq0xG
=piva
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
Pep
Guest
Posts: n/a
 
      09-09-2009
On Sep 9, 3:03*pm, Michael Tsang <(E-Mail Removed)> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
>
> Pep wrote:
> > Think it is about time I got this straight in my head, with the help
> > of you guys of course

>
> > So a struct is a collection of data members as is a class, where a
> > class default scope is private and a struct default scope is public.
> > All well and good so far.

>
> > If I send the address of a struct or class to a function within my
> > program everything is well and good, obviously all of the class/
> > struct, i.e. the members, methods and vtable exist in my program.

>
> > Now here is the point I get a little fuzzy, what happens if I want to
> > serialize the struct/class. Assuming I send the address of the struct/
> > class and it's size() as parameters to my serialize function, I can
> > effectively write the size() number of bytes from the address
> > parameter to a file. What happens with the methods and vtable of a
> > struct/class when you serialize it?

>
> > I know that I can easily allocate a region of memory and cast it to a
> > struct/class and effectively use it as an instantiated instance of the
> > struct/class. So I suspect the obvious answer is that the methods and
> > vtable are discarded and reapplied when the memory region when cast
> > against a struct/class.

>
> > Presumably, the only way a struct/class can be deserialized is by a
> > function that knows the declaration of the struct/class?

>
> > So is what I outlined correct in terms of the c++ standard or only for
> > certain c++ compilers that may have extended things?

>
> Difference between class and struct:
> class has private access level, struct has public access level;
> class can be used to declare template parameter, struct can't.
>
> When you serialize (dump) a class into a file, the content will be the
> internal representation of a class (including any v-table, padding bytes,
> etc.). Therefore, dumping any class with pointers (including all classes
> with virtual members and inheritance, as they have a hidden pointer at the
> front) is useless. Deserializing a class (providing it does not have any
> pointers) is similar to serializing a class, you only need to pass the
> pointer and the size. Therefore, to deserialize a class, you just need to
> call fread(3).
>
> Here is the criteria for classes which are safe to serialize:
>
> - - It does not contain any pointers/references.
> - - It does not contain any virtual functions, including virtual functions
> inherited by base classes.
> - - It does not have virtual base classes, including virtual base classes
> indirectly inherited by base classes.
> - - It does not contain any classes which aren't safe to serialize.
>
> To deserialize, the deserializing program and the serializing program must
> be ABI-compatible in order to obtain useful results.
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
>
> iEYEARECAAYFAkqntZcACgkQG6NzcAXitM9EwgCdGrEe+FgSYN IxOMEA+2bO+Uqe
> sFcAnjgD8glOMGKSu2+4IO+lYEBaq0xG
> =piva
> -----END PGP SIGNATURE-----


Thanks, it is the virtual stuff that always confuses me
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-10-2009
On Sep 9, 3:47 pm, Pep <(E-Mail Removed)> wrote:
> Think it is about time I got this straight in my head, with
> the help of you guys of course


> So a struct is a collection of data members as is a class,
> where a class default scope is private and a struct default
> scope is public. All well and good so far.


More practically, there aren't really struct's in C++. Both
class and struct keywords defined classes.

Note too that they can be used more or less interchangeably:

class Toto ; // foreward declaration
// ...
struct Toto
{
// ...
} ;

> If I send the address of a struct or class to a function
> within my program everything is well and good, obviously all
> of the class/ struct, i.e. the members, methods and vtable
> exist in my program.


That's the compiler's problem, not yours.

> Now here is the point I get a little fuzzy, what happens if I
> want to serialize the struct/class.


You have to write the code to do it.

> Assuming I send the address of the struct/ class and it's
> size() as parameters to my serialize function, I can
> effectively write the size() number of bytes from the address
> parameter to a file.


You can, but it doesn't necessarily mean anything. Except for a
few degenerate cases, you won't be able to reliably reread the
data.

> What happens with the methods and vtable of a struct/class
> when you serialize it?


What do you want to happen to them?

The member functions are just that---functions, and are compiled
into the application. You can't write them to disk, nor reread
them; there's practically no way of serializing them. In most
implementations, the vtable is also part of "code"; all that the
class contains is a pointer to it.

When serializing data (regardless of the type of data), you need
to know the type in order to reread it successfully. This
knowledge can come from the structure of the file (at this
location in the file, there is always a Toto), or by writing
some sort of type identifier in front of the data, and reading
it dynamically.

> I know that I can easily allocate a region of memory and cast
> it to a struct/class and effectively use it as an instantiated
> instance of the struct/class.


No you can't. That's undefined behavior, and typically doesn't
work.

> So I suspect the obvious answer is that the methods and vtable
> are discarded and reapplied when the memory region when cast
> against a struct/class.


> Presumably, the only way a struct/class can be deserialized is
> by a function that knows the declaration of the struct/class?


Again, that's true for any type, not just class types.

> So is what I outlined correct in terms of the c++ standard or
> only for certain c++ compilers that may have extended things?


It's completely false.

--
James Kanze
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-10-2009
On Sep 9, 4:03 pm, Michael Tsang <(E-Mail Removed)> wrote:

[...]
> Difference between class and struct:
> class has private access level, struct has public access
> level; class can be used to declare template parameter, struct
> can't.


The word class has two meanings: it is a keyword (which can be
used to declare a template type parameter, in which case it is
synonymous with typename), but the standard also talks about
class types. I suspect that this is often a source of
confusion---a class type can be declared with any of the
keywords class, struct or union (but there are a number of
constraints regarding the use of union).

> When you serialize (dump) a class into a file, the content
> will be the internal representation of a class (including any
> v-table, padding bytes, etc.).


Serializing is *not* dumping. Dumping is only valid for a very
small set of types, at least if you want to reread the data
later. (Dumping is usually only done in hex format, for
information purposes.)

> Therefore, dumping any class with pointers (including all
> classes with virtual members and inheritance, as they have a
> hidden pointer at the front) is useless. Deserializing a class
> (providing it does not have any pointers) is similar to
> serializing a class, you only need to pass the pointer and the
> size. Therefore, to deserialize a class, you just need to call
> fread(3).


That's only true for a very limited number of cases: character
types, and arrays of character types.`

> Here is the criteria for classes which are safe to serialize:


You can serialize any type you want, including all class types,
but it's up to you to write the code to do it.

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
(: Pointer to struct withing pointer to struct :) Zero C Programming 16 11-19-2005 01:27 AM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments