LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-27-2004, 09:13 AM   #1
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Rep: Reputation: 30
Variable length objects


Hi. Suppose I write a string object to a file with

Code:
file.write(reinterpret_cast <char*> &myObject, sizeof(myObject) );
Now, I can read it with

Code:
file.read(reinterpret_cast <char*> &myObject, sizeof(myObject) );
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.

Thanks.
 
Old 10-27-2004, 02:08 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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.
 
Old 10-28-2004, 04:40 AM   #3
deveraux83
Member
 
Registered: Jul 2003
Location: Malaysia
Distribution: Red Hat, Slackware 9.1
Posts: 76

Rep: Reputation: 15
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.
 
Old 10-28-2004, 12:50 PM   #4
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
I know all of this. I forgot to mention that myObject is an std::string object. In that case, I think the code will not work. Am I right?
 
Old 10-28-2004, 12:56 PM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
Quote:
I think the code will not work. Am I right?
You are right, for reasons stated. lol
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java objects zaicheke Programming 3 03-23-2005 06:21 AM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM
Mounting objects more easily.... r_jensen11 Linux - Hardware 5 11-14-2003 03:50 PM
lost objects gurra59 Linux - Newbie 4 09-07-2003 04:29 PM
Objects in Java? mikeshn Programming 0 06-11-2002 10:21 AM


All times are GMT -5. The time now is 05:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration