zl2k
9/26/2008 1:45:00 PM
On Sep 26, 9:02 am, Triple-DES <DenPlettf...@gmail.com> wrote:
> On 26 Sep, 14:40, zl2k <kdsfin...@gmail.com> wrote:
>
> > On Sep 26, 8:23 am, Triple-DES <DenPlettf...@gmail.com> wrote:
> > > On 26 Sep, 13:37, zl2k <kdsfin...@gmail.com> wrote:
> > > > On Sep 26, 2:17 am, Triple-DES <DenPlettf...@gmail.com> wrote:
> > > > > On 26 Sep, 05:04, zl2k <kdsfin...@gmail.com> wrote:
> > > > > > hi, there
> > > > > > Here is a simplified piece of code of my program, it compiles and runs
> > > > > > fine. However, valgrind shows it has uninitialized problem. What I am
> > > > > > doing wrong?
>
> [snip]
>
>
>
> > > > > Try the following:
> > > > > int data2size = sizeof(Data2);
> > > > > int membersize = sizeof(int) + sizeof(bool) + sizeof(double)*16;
>
> > > > > What are these values on your system?
> > > > > Does that tell you anything ? :)
>
> > > > > DP
>
> > > > It's different! To make it simpler, the Data2 will only contains an
> > > > int and a bool, the data2size is 8 and membersize is 5. I am fine if
> > > > using 5 for output. The sizeof(ClassType) is bad, is it? Thanks a lot.
> > > > zl2k
>
> > > You're onto something, but your conclusion is wrong I think. What I
> > > wanted to show you was that the Data2 class may very well be larger
> > > than the sum of its members (sizeof returns the number of bytes of the
> > > object). So what are those extra bytes?
>
> > > Put simply, they are "padding" inserted by the compiler so that it may
> > > access the members of the class more efficiently. This is commonly
> > > referred to as _alignment_. Naturally when you take the address of the
> > > object and interpret it as a char*, you will expose the padding bytes.
>
> > > Data2 possible object layout:
> > > [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ][ 7 ]
> > > [ int (4 bytes) ][bool][ padding ]
>
> > > The padding could be set to a special value by the compiler to
> > > correspond to "uninitialized memory", or it could simply be garbage.
> > > Hence, your binary file will have bytes with an unspecified value
> > > written to it.
>
> > > Class member alignment may vary depending on architecture and compiler
> > > version, and even the settings of the compiler. This is covered by
> > > James Kanze's post.
>
> > > DP
>
> > So both implements are correct and the valgrind is false alarming,
> > right? Basically, as long as my reading of the file using the same
> > sizeof() as the writting, I'll be fine. But I am still facing risk if
> > the file is going to be read by other systems later on if using
> > sizeof(ClassType). The conclusion is draw is telling the system the
> > exact length of the ClassType "manually". Hope I get it right. Thanks
> > again and this forum helps me a lot.
> > zl2k
>
> Not quite. Manually calculating the size of the members won't help if
> there is padding _between_ the members. Note also that valgrind is not
> really giving a false alarm since the padding bytes are in fact
> uninitialized.
>
> DP
The valgrind does give error message when I put the double[] back even
I "manually" tell the length of the ClassType. I can write each
element sequentially and manually for the ClassType, is that what I am
expected to do? It doesn't look like a smart way. Is there a standard
way to output the structure to the binary file? The c++ document of
microsoft uses myfile.write((char*) classPointer,
sizeof(ClassType)*LengthOfArray). But I am using gun c++ in linux.
What is the correct way to do this safe that the output file can be
correctly read in any system? I am confused again.
zl2k