 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
mbricker Guest
|
Posted: Wed May 09, 2007 7:05 pm Post subject: Reading structures with ifstream read function |
|
|
I have a question concerning any changes between version 4.0 and 6.0
concerning the way data types are written and read in a structure. I
currently have a data structure with a TDateTime, int, followed by
some char[25] strings and some doubles. When I read the file (using
version 4.0)containing this structure , I get the proper information.
But when I read this same file with version 6.0, the structure looks
like a mess. I am wondering if there has been any changes the way
data type is read. Thanks |
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Wed May 09, 2007 7:21 pm Post subject: Re: Reading structures with ifstream read function |
|
|
mbricker <michaelmbricker (AT) aol (DOT) com> writes:
| Quote: | I have a question concerning any changes between version 4.0 and 6.0
concerning the way data types are written and read in a structure. I
currently have a data structure with a TDateTime, int, followed by
some char[25] strings and some doubles. When I read the file (using
version 4.0)containing this structure , I get the proper information.
But when I read this same file with version 6.0, the structure looks
like a mess. I am wondering if there has been any changes the way
data type is read. Thanks
|
If you're writing a structure as binary information, by simply
streaming out sizeof(mystruct) bytes, starting at &mystruct, then you
have a very touchy problem. The data layout is not just compiler
dependent, but also compiler-option dependent.
The variables do not have to be in any particular order, and there is
alignment issues (padding) and also "other" data that may change from
version to version. You should not expect binary compatibility
between versions, as you have already discovered. This isn't just a
problem of BCB, but of any c++ compiler.
You may be able to make it work if you explicitly set the alignment
when you compile the code that reads and writes the data, and also to
do some checking that the size is what you expect, etc. But a better
solution is to marshal the data in a portable format. One way is to
stringify it (with each field prefixed by its length), or to use some
sort of CDR marshalling encoding scheme. CDR is a standard
representation that is platform independent. (CDR is what CORBA uses
for its distributed data sharing.)
There are some options available for externalizing your data in more
reliable formats. The freely available ACE library
(http://www.cs.wustl.edu/~schmidt/ACE.html) has CDR streams
implemented, if you care to use ACE. It's a nice library, though
rather "large".
There are other options too, such as Boost's serialization library
(http://www.boost.org/libs/serialization/doc/index.html) which allows
you to read/write data structures, or the XTL C++ library for
externalizing code (downloadable here: http://xtl.sourceforge.net/) in
various formats, including CDR.
--
Chris (TeamB); |
|
| Back to top |
|
 |
dhoke Guest
|
Posted: Wed May 09, 2007 10:47 pm Post subject: Re: Reading structures with ifstream read function |
|
|
"Chris Uzdavinis (TeamB)" <chris (AT) uzdavinis (DOT) com> wrote in message
news:86hcqmt6uw.fsf (AT) explicit (DOT) atdesk.com...
| Quote: | mbricker <michaelmbricker (AT) aol (DOT) com> writes:
The variables do not have to be in any particular order,
|
Anyone,
I've already found I'm somewhat out-of-date (the ARM was the last "standard"
I had some familiarity with, and that was over a decade ago), but,
does the Standard no longer specify that order within the same access
modifier section (those public:/protected:/private: thingies), _is_ supposed
to be in a known (ascending address) order????
The ARM used to specify that was to be the case.
Chris,
What exactly do you mean by "The variables do not have to be in any
particular order"?
Thanks. |
|
| Back to top |
|
 |
Thomas Maeder [TeamB] Guest
|
Posted: Thu May 10, 2007 12:12 am Post subject: Re: Reading structures with ifstream read function |
|
|
"dhoke" <dhoke.nojunk@east-shore.com> writes:
| Quote: | I've already found I'm somewhat out-of-date (the ARM was the last "standard"
I had some familiarity with, and that was over a decade ago), but,
does the Standard no longer specify that order within the same access
modifier section (those public:/protected:/private: thingies), _is_ supposed
to be in a known (ascending address) order????
|
It still does.
| Quote: | What exactly do you mean by "The variables do not have to be in any
particular order"?
|
I'm sure that he meant variables with an intervening access specifier. |
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Thu May 10, 2007 1:29 am Post subject: Re: Reading structures with ifstream read function |
|
|
"dhoke" <dhoke.nojunk@east-shore.com> writes:
| Quote: | Chris,
What exactly do you mean by "The variables do not have to be in any
particular order"?
|
I just mean that the standard does not dictate anything about how the
compiler shall implement the language. It says what the program
behavior must be, but as long as that behavior is met, the vendor
implementation is relatively free to implement it however desired.
For one thing, if a class has a virtual function, then it has a
vtbl-ptr inserted in the object, but where? (Well, that too is an
implementation detail, but all compilers that I know of implement
virtual fucntions with a vtbl.) If multiple inheritance is used, then
the layout can become convoluted as well, as different bases are in
"some" order, and may repeat, etc.
There are probably other attributes that affect the layout, but again,
that's up to the vendor.
--
Chris (TeamB); |
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Thu May 10, 2007 1:36 am Post subject: Re: Reading structures with ifstream read function |
|
|
maeder (AT) glue (DOT) ch (Thomas Maeder [TeamB]) writes:
| Quote: | What exactly do you mean by "The variables do not have to be in any
particular order"?
I'm sure that he meant variables with an intervening access specifier.
|
Thank you for giving me the benefit of the doubt, but I wasn't
actually thinking of access specifiers. I was, however, thinking of
multiple/virtual base classes.
--
Chris (TeamB); |
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Thu May 10, 2007 1:47 am Post subject: Re: Reading structures with ifstream read function |
|
|
maeder (AT) glue (DOT) ch (Thomas Maeder [TeamB]) writes:
| Quote: | "dhoke" <dhoke.nojunk@east-shore.com> writes:
I've already found I'm somewhat out-of-date (the ARM was the last
"standard" I had some familiarity with, and that was over a decade
ago), but, does the Standard no longer specify that order within
the same access modifier section (those public:/protected:/private:
thingies), _is_ supposed to be in a known (ascending address)
order????
It still does.
|
My mistake. Where does it say that?
--
Chris (TeamB); |
|
| Back to top |
|
 |
Thomas Maeder [TeamB] Guest
|
Posted: Thu May 10, 2007 2:07 am Post subject: Re: Reading structures with ifstream read function |
|
|
Chris Uzdavinis (TeamB) <chris (AT) uzdavinis (DOT) com> writes:
| Quote: | I've already found I'm somewhat out-of-date (the ARM was the last
"standard" I had some familiarity with, and that was over a decade
ago), but, does the Standard no longer specify that order within
the same access modifier section (those public:/protected:/private:
thingies), _is_ supposed to be in a known (ascending address)
order????
It still does.
My mistake. Where does it say that?
|
In the 1998 Standard, this is said in §9.2/12:
Nonstatic data members of a (non-union) class declared without an
intervening access-specifier are allocated so that later members have
higher addresses within a class object.
To be honest, I was guessing when I wrote that it still does, but I
lack the imagination to assume that this could have been changed
since. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|