ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Now my question is, when I write the file, the size will be determined by the length of the string in the object. But when reading it, how does the compiler know how many bytes to read from the file? Shouldn't the size of each object be different.
Chances are that when you think you are resizing your object, you're actually resizing an area of heap memory which is pointed to by a pointer inside your object. This means that when you read or write your object, it's always the same length, but when you read it that pointer is not going to point to something you want. You're going to have to figure out some other way to serialize your object.
Assuming that 'MyObject' is of fixed length (i.e. no pointers that get allocated memory), then your code should work fine. What reinterpret_cast is doing in your code is just changing the way c++ interprets the memory location, i.e. instead of being the variables in that class, it turns it into a large string of characters which can be written out to file (This is how all data is saved anyway including integers/floats when it's saved in binary form).
The reason why read knows what length to read is because of the fixed length, i.e. the sizeof(MyObject) doesn't change. If you however have say a character array defined as a pointer and then allocated as the program is executed, then even your write could will not be 'writing' properly as sizeof(MyObject) will NOT return the length of the character array because it is originally defined as a pointer that is pointing to a location in memory. So, just be careful with pointers in general.
Another point with pointers is that although you will store the correct memory location pointed by the pointer, the next time you run the program, there's no reason why it would be put into the same memory space and hence your pointer (read fromt he file) might well be pointing outside your programs memory space which will cause a segmentation fault.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.