Difference between struct and class and pointer to member data

Discussion in 'C++' started by Pep, Sep 9, 2009.

  1. Pep

    Pep Guest

    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?
     
    Pep, Sep 9, 2009
    #1
    1. Advertising

  2. -----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+FgSYNIxOMEA+2bO+Uqe
    sFcAnjgD8glOMGKSu2+4IO+lYEBaq0xG
    =piva
    -----END PGP SIGNATURE-----
     
    Michael Tsang, Sep 9, 2009
    #2
    1. Advertising

  3. Pep

    Pep Guest

    On Sep 9, 3:03 pm, Michael Tsang <> 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+FgSYNIxOMEA+2bO+Uqe
    > sFcAnjgD8glOMGKSu2+4IO+lYEBaq0xG
    > =piva
    > -----END PGP SIGNATURE-----


    Thanks, it is the virtual stuff that always confuses me :)
     
    Pep, Sep 9, 2009
    #3
  4. Pep

    James Kanze Guest

    On Sep 9, 3:47 pm, 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.


    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
     
    James Kanze, Sep 10, 2009
    #4
  5. Pep

    James Kanze Guest

    On Sep 9, 4:03 pm, Michael Tsang <> 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
     
    James Kanze, Sep 10, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,214
  2. beetle
    Replies:
    2
    Views:
    911
    beetle
    Jan 25, 2005
  3. Zero
    Replies:
    16
    Views:
    657
    Barry Schwarz
    Nov 19, 2005
  4. Stephen Howe
    Replies:
    2
    Views:
    290
    Stephen Howe
    Nov 6, 2012
  5. somenath
    Replies:
    10
    Views:
    283
    James Kanze
    Jul 2, 2013
Loading...

Share This Page