Thread: Writing classes of dynamic size

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    12

    Writing classes of dynamic size

    Hello.
    So first of all, I'm used to C, but not C++, so I probably need a little bit more explanation than is normal.

    Here is my question:
    Suppose I have a class like this

    Code:
    class Hello
    {
    public:
    Hello() {};
    ~Hello() {};
    char *str;
    float x;
    };
    Furthermore, I have a program which, in the end, outputs a Hello object in direct binary form. I am wondering how this can be done considering that we do not know, a priori, how large the object will be.

    Thanks for any help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That depends on what you mean by "pure binary form", I suppose. A Hello object will have probably eight bytes in it -- four for the char pointer, and four for the float. (I would imagine that on a 64-bit machine, it will have twelve.) There's some uncertainty there, but not because of the contents -- on any given machine, every Hello object will be of the same size.

    Now if you want to write out the contents pointed to by str, that's not a big problem either, as strings must end with a 0 byte (and cannot contain a 0 byte inside of it).

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    12
    I don't think this quite solves my problem, but that's because I was a little unclear (sorry).

    What I really want to do is write this out, and then be able to read it back in in the same format. So for instance, suppose I make a program foo.cpp which creates a Hello object called obj, sets x=3.14 and sets str to "Hello world."

    Then, I want to write out obj in binary (32 bit) to a file.

    Next, I have a program bar.cpp which has the same class definition for Hello. I want to create a Hello object on obj_in, and I want to read the binary directly from the output from foo.cpp.

    This is easy to do if Hello is of constant size thanks to the sizeof operator, but I don't know how to do it if it isn't of fixed size.

    Thanks!

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    classes are never of variable size.

    str isn't "in" hello. a pointer to str is in hello.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Would this not be of variable size?

    Code:
    class Hello
    {
    public:
    Hello() {};
    ~Hello() {};
    string str;
    float x;
    };
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    no.

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
      std::string asdf = "asdf";
      std::string asdfasdfasdfasdf = "asdfasdfasdfasdf";
      std::cout<<  sizeof(std::string)<<'\n';
      std::cout<<  sizeof(asdf)<<'\n';
      std::cout<<  sizeof(asdfasdfasdfasdf)<<'\n';
      system("pause");  
    	return 0;
    }
    exactly as above, std::string contains a pointer to an array of chars, not an array of chars.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    OK, so that makes marshaling instances all the more a pita.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    but it makes computers much more efficient.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Dino View Post
    OK, so that makes marshaling instances all the more a pita.
    Well, if you're marshalling (converting to a format for storage or transmission) then it is generally necessary to assume a maximum size (eg length of a string), somehow marshal the length of that string, or use some protocol that identifies beginning and end.

    Computers are not omnipotent: a computer is actually an ignoramus that does certain things very fast. If you want a computer to handle arbitrary data, you need to design an unambiguous method of handling that data, and then program the computer to do so.

    For an arbitrary length string (or an arbitrary length anything) that means informing the computer what the length is, or defining some means by which the length is computed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Does C++ not share the Java Serializable concept?
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    it is not necessary to specify both a beginning and an end, only length. the next beginning can be inferred from the previous end.
    Code:
    class Hello
    {
    public:
      Hello() :
        str(0)
      {
      }
      Hello(char *_str,float _x):
        str(_str),
        x(_x)
      {
      }
      ~Hello() 
      {
        if(str)
        {
          delete str;
        }
      }
      char *str;
      float x;
    };
    
    int serializeHello(Hello& h,const char *filePath)
    {
      char*index=h.str;
      while(*index)
      {
        index++;
      }
      unsigned int strLen = (unsigned int)index-(unsigned int)h.str;  
      std::ofstream out(filePath,std::ios_base::out|std::ios_base::binary);
      if(!out.is_open())
      {
        return -1;
      }
      out.write((char*)&strLen,sizeof(unsigned int));  
      if(strLen++)
      {
        out.write(h.str,strLen);
      }
      out.write((char*)&h.x,sizeof(float));
      return out.tellp();
    }
    
    Hello deserializeHello(const char *filePath)
    {
      std::ifstream in(filePath,std::ios_base::in|std::ios_base::binary);
      unsigned int i;
      in.read((char*)&i,sizeof(unsigned int));
      char* str=0;
      if(i++)
      {
        str = new char[i];
        in.read(str,i);
      }
      float x;
      in.read((char*)&x,sizeof(float));
      return Hello(str,x);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    { 
      char *asdf = "asdf";   
      char*str = new char[5];
      memcpy(str,asdf,5);
      
      
      Hello h(str,1.234);
      if(serializeHello(h,"hello")>0)
      {
        Hello h2 = deserializeHello("hello");
      }
      
      system("pause");  
    	return 0;
    }

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Dino View Post
    Does C++ not share the Java Serializable concept?
    The short answer is "no".

    That question is sort of meaningless here, as the Java Serializable interface does not magically make classes serializable. Java classes that implement the Serializable interface are still required to implement the mechanism by which the class is serialized - i.e. they have to explicitly perform the marshalling in some way.


    Quote Originally Posted by m37h0d View Post
    it is not necessary to specify both a beginning and an end, only length.
    Often true, but not always.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Thanks for the example m37h0d, and clarification grumpy. (Yes, interface, not concept)
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Dynamic Array Size
    By minignaz in forum C Programming
    Replies: 11
    Last Post: 12-12-2003, 06:05 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. total size of dynamic memory allocated array
    By trekker in forum C Programming
    Replies: 10
    Last Post: 03-10-2002, 12:59 PM
  5. File Size and File Size on Disk
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-15-2001, 08:03 PM