Fwd: RE: [Dev-C++] [Dev-C++]: Errors in structures
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Johannes P. <joh...@pr...> - 2001-07-04 04:57:29
|
>Date: Tue, 03 Jul 2001 14:46:07 +0200
>To: dev...@li...
>From: Johannes Pretorius <joh...@pr...>
>Subject: RE: [Dev-C++] [Dev-C++]: Errors in structures
>Cc: dev...@li...
>Bcc: dev...@li...
>
>Please for give if I am incorrect but does a union not allocate the memory
>space
>of the biggest type inside it. for instace in your example the biggest
>type is the Align (I think) so the whole
>union will be that size. Note that a union enables varaibles to share a
>memory block. and the last one used has writen into that
>memory block .. IF I am correct , changes are I am understanding this wrong.
>
>If so the problem still persist that the sizeof() property that is suppose
>to give
>you a result of the memory size that is take up for the variable is not
>correct, AND if this
>value is used in READING a file for instance.. This will result that if a
>file was writen in a diffent program
>by a different compiler. You will not read the exact amount of bytes into
>your structure to corrosponde to
>your structure. Meaning you dont have the correct values in.
>
>Example :
>A file header writen in MSVC looks as follows in the start of the file
>(note comments in [] are not in file)
>[START OF FILE]34^^^^^^^^[8 BYTES]Hallo world^^^^^[16 BYTES]1[1 BYTES]0[1
>BYTES]
>
>The structure for this file looks something like (not correct maybe but
>this is just to give the Idea out)
>
>struct header{
> int version;
> char comment[16];
> char code;
> char sub_code;
>}oHeader;
>
>Now if this is writen to the File we do it as
>
>fwrite(&oHeader,sizeof(oHeader),1,outfile);
>
>Meaning the size that we have occrding to our compiler.,.,
>
>NOW -- we go and write a App to read this in DevCpp and we have the same
>structure BUT
>DevCpp (or the gcc or mingW) has a different value of the sizeof() so it
>will end up as follows
>
>we read the file in
>
>fread(&oHeader,sizeof(oHeader),1,infile);
>
>and we expect the result to look the same. BUT the sizeof this program
>returned 24 forinstance and
>
>This will result in the past that the LAST byte wont be read..or wont be
>read into the correct value
>considering that this is the same (almost ) as doing a memcpy from the
>file to the structure memory alocation..
>
>Please help me or correct me if I cam wrong..
>
>I hope I am wrong and misunderstand this..
>I do believe the packed solution may help us here.,
>
>
>Thanks for All your help
>
>Johannes
>
>
>
>
>
>At 14:29 03/07/2001 +0200, you wrote:
>>This is not an error. A structure's space in memory is not the summary
>>of the sizeof() of everyone of its members. It may occupy more memory
>>due to compiler's memory management technique. For more information read
>>section 8.7 in K&R 2. For example a compiler's memory management can be
>>a connected list of structs (compilers, operating systems e.t.c. are
>>written in C/C++ too). So in the example of K&R it has a list of unions
>>connected structs:
>>
>>typedef long Align;
>>
>>union header
>>{
>> struct
>> {
>> union header *ptr; /* points to next block */
>> unsigned size;
>> }s;
>>
>> Align x;
>>};
>>
>>typedef union header Header;
>>
>>
>>Ioannis
>>
>>* Ioannis Vranos
>>* Programming pages: http://www.noicys.f2s.com
>>* Alternative URL: http://run.to/noicys
>>
>>
>>-----Original Message-----
>>From: dev...@li...
>>[mailto:dev...@li...] On Behalf Of Jeethu
>>Rao
>>Sent: Tuesday, July 03, 2001 5:52 AM
>>To: Dev CPP Mailing List
>>Subject: [Dev-C++] [Dev-C++]: Errors in structures
>>
>>
>>Hi,
>>
>>I've noticed this strange little Bug.I'm not certain weather its a bug
>>or a feature.
>>
>>The size of this structure is actually 13 bytes.
>>
>>But sizeof(header) says its 16.
>>
>>struct tea_hdr
>> {
>> char jte[3];
>> unsigned char version;
>> DWORD infile_size;
>> DWORD infile_crc;
>> unsigned char fname_sz;
>> }header;
>>
>>No, if I comment out the fname_sz member,
>>
>>struct tea_hdr
>> {
>> char jte[3];
>> unsigned char version;
>> DWORD infile_size;
>> DWORD infile_crc;
>> //unsigned char fname_sz;
>> }header;
>>
>>sizeof(header) returns 12.
>>
>>Is the compiler trying to align structure on DWORD boundaries?
>>
>>How do I get rid of this Bug/Feature ?
>>
>>Thnx,
>>Jeethu Rao
>>
>>
>>_______________________________________________
>>Dev-cpp-users mailing list
>>Dev...@li...
>>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>>
>>_______________________________________________
>>Dev-cpp-users mailing list
>>Dev...@li...
>>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|