Thread: 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-03 07:07:00
|
>Date: Tue, 03 Jul 2001 09:16:19 +0200
>To: dev...@li...
>From: Johannes Pretorius <joh...@pr...>
>Subject: RE: [Dev-C++] [Dev-C++]: Errors in structures
>
>
>Action : Response
>Mood : Friendly
>Subject : Data Type mismatch in C/C++ gcc ,mingW32 compiler
>
>Considering this Feature . It is making it hard to read a fixed structure
>size from a file or a memory stream for
>instance for the compiler has decided that the structure must be
>optimized. Meaning that if a other compiler
>created the file then the program will not read the correct size of the
>contents in to contain all the data.
>
>Considering this if I turn all compiler option of from the DevC++ IDE the
>result is still the same.
>else I dont know or cant see how this can be usefull in complaince wise..
>
>Or am I missing something ???
>
>Thanks for your help so far
>
>Johannes
>
>
>
>At 11:35 04/07/2001 +0530, you wrote:
>>It is not a bug, it is compiler feature. For some optimizations some c++
>>compilers adjust the size of structue to the multiple of 8.
>>
>>
>>Hi there
>>-------------
>>I am sorry to say that I dont have an answer for you . The LibC help file
>>does say that the Data Types can vary from Compiler
>>to Compiler But I WOULD like to show what I have also picked up with your
>>Code
>>
>>//Snippet - START
>>#include <iostream.h>
>>#include <stdlib.h>
>>#include <limits.h>
>>
>>struct tea_hdr{
>> int version; //-- 1
>> char jte[3]; // -- 2
>> //char fname_sz; // -- 3
>> //char infile_size; // -- 4
>>// char infile_crc; // -- 5
>>
>> }header;
>>
>>
>>int main()
>>{
>> cout<<"Char Minimum : "<<CHAR_MIN<<endl;
>> cout<<"Char Max : "<<CHAR_MAX<<endl;
>> cout<<"Unsigned Char Max : "<<UCHAR_MAX<<endl;
>> cout <<"Header : "<<sizeof(header)<<endl;
>> cout<<"Struct : "<<sizeof(tea_hdr)<<endl;
>> system("PAUSE");
>> return 0;
>>}
>>
>>//Snippet END
>>
>>Now if you compile it as follow the Size of the Structure is 8 (The integer
>>(5) and the char[3]) That is ok.,
>>
>>Now remove the comment before item 3 and compile and run Again.. The size
>>is STILL the same ?? Is this just my
>>PC.. must I reboot.. BUT there is more.
>>
>>Now uncomment item 4 .. Compile and RUN.. SIZE is 12 that is 4 bytes
>>extra,But there is still more
>>
>>Now uncomment item 5 and compile and run.. IT stays at 12 bytes again..
>>This seems to be variable
>>items count related. I have sifted the vars around and nothing is with
>>there posisions..
>>Also it only starts when MIXING variables in the structure.. I have a
>>feeling that this is something
>>that can be compiler related.. BUT what I did notice in the 'stddef.h' File
>>is that the MingW32 oaks have
>>a prob with the defs of the Gcc compiler and has added some macro to fix it
>>.. I wonder if maybe something else
>>also was missed that can relate to this. That the problem can maybe be at
>>the MingW32 project.
>>
>>I don't have any answer's just maybes. but maybe it can help you in getting
>>to a answer For IF I cam correct you are trying to
>>read the information into the structure from a File .. like the header of a
>>MP3..
>>
>>I dont have any other Ideas atound this but I hope you get a solution
>>
>>Good luck
>>
>>Johannes
>>
>>
>>
>>
>>
>>
>>At 09:22 03/07/2001 +0530, you wrote:
>> >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
>>
>>
>>_______________________________________________
>>Dev-cpp-users mailing list
>>Dev...@li...
>>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Johannes P. <joh...@pr...> - 2001-07-03 07:07:04
|
>Date: Tue, 03 Jul 2001 09:16:19 +0200
>To: dev...@li...
>From: Johannes Pretorius <joh...@pr...>
>Subject: RE: [Dev-C++] [Dev-C++]: Errors in structures
>
>
>Action : Response
>Mood : Friendly
>Subject : Data Type mismatch in C/C++ gcc ,mingW32 compiler
>
>Considering this Feature . It is making it hard to read a fixed structure
>size from a file or a memory stream for
>instance for the compiler has decided that the structure must be
>optimized. Meaning that if a other compiler
>created the file then the program will not read the correct size of the
>contents in to contain all the data.
>
>Considering this if I turn all compiler option of from the DevC++ IDE the
>result is still the same.
>else I dont know or cant see how this can be usefull in complaince wise..
>
>Or am I missing something ???
>
>Thanks for your help so far
>
>Johannes
>
>
>
>At 11:35 04/07/2001 +0530, you wrote:
>>It is not a bug, it is compiler feature. For some optimizations some c++
>>compilers adjust the size of structue to the multiple of 8.
>>
>>
>>Hi there
>>-------------
>>I am sorry to say that I dont have an answer for you . The LibC help file
>>does say that the Data Types can vary from Compiler
>>to Compiler But I WOULD like to show what I have also picked up with your
>>Code
>>
>>//Snippet - START
>>#include <iostream.h>
>>#include <stdlib.h>
>>#include <limits.h>
>>
>>struct tea_hdr{
>> int version; //-- 1
>> char jte[3]; // -- 2
>> //char fname_sz; // -- 3
>> //char infile_size; // -- 4
>>// char infile_crc; // -- 5
>>
>> }header;
>>
>>
>>int main()
>>{
>> cout<<"Char Minimum : "<<CHAR_MIN<<endl;
>> cout<<"Char Max : "<<CHAR_MAX<<endl;
>> cout<<"Unsigned Char Max : "<<UCHAR_MAX<<endl;
>> cout <<"Header : "<<sizeof(header)<<endl;
>> cout<<"Struct : "<<sizeof(tea_hdr)<<endl;
>> system("PAUSE");
>> return 0;
>>}
>>
>>//Snippet END
>>
>>Now if you compile it as follow the Size of the Structure is 8 (The integer
>>(5) and the char[3]) That is ok.,
>>
>>Now remove the comment before item 3 and compile and run Again.. The size
>>is STILL the same ?? Is this just my
>>PC.. must I reboot.. BUT there is more.
>>
>>Now uncomment item 4 .. Compile and RUN.. SIZE is 12 that is 4 bytes
>>extra,But there is still more
>>
>>Now uncomment item 5 and compile and run.. IT stays at 12 bytes again..
>>This seems to be variable
>>items count related. I have sifted the vars around and nothing is with
>>there posisions..
>>Also it only starts when MIXING variables in the structure.. I have a
>>feeling that this is something
>>that can be compiler related.. BUT what I did notice in the 'stddef.h' File
>>is that the MingW32 oaks have
>>a prob with the defs of the Gcc compiler and has added some macro to fix it
>>.. I wonder if maybe something else
>>also was missed that can relate to this. That the problem can maybe be at
>>the MingW32 project.
>>
>>I don't have any answer's just maybes. but maybe it can help you in getting
>>to a answer For IF I cam correct you are trying to
>>read the information into the structure from a File .. like the header of a
>>MP3..
>>
>>I dont have any other Ideas atound this but I hope you get a solution
>>
>>Good luck
>>
>>Johannes
>>
>>
>>
>>
>>
>>
>>At 09:22 03/07/2001 +0530, you wrote:
>> >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
>>
>>
>>_______________________________________________
>>Dev-cpp-users mailing list
>>Dev...@li...
>>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
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
|
|
From: Johannes P. <joh...@pr...> - 2001-07-04 05:22:23
|
I realize now that my example with the 24 bytes is correct for it is 24
bytes. But if you remove the sub_code from the structure and
have two entries of the structure in a binary file forinstance.. like
[START OF FILE]34^^^^^^^^[8 BYTES]Hallo world^^^^^[16 BYTES]1[1 BYTES]0[1
BYTES][START OF 2 STRUC]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;
}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
we will start reading the beginning of the next entry of the second
structure...
Now this will make my example more true..
I hope I understand this incorrect and that somebody can please explain
this problem to me in more detail...
Thanks
Johannes
At 07:08 04/07/2001 +0200, you wrote:
>>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
>
>
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Elias P. <eli...@ao...> - 2001-07-04 13:25:52
|
... > 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 > we will start reading the beginning of the next entry of the second > structure... > > Now this will make my example more true.. > > I hope I understand this incorrect and that somebody can please explain > this problem to me in more detail... You understand correct: it is almost always a bad idea to do something like fread(&oHeader,sizeof(oHeader),1,infile); // bad ! If you want to store data into a file, and there is any chance this file might be read by another application, or the same application, but in a different compile, don't do this. Use an existing file format, or create your own instead. It should exactly specify how the data are stored. Then both applications for reading and writing can look at the specifications and read/write it accordingly. If you still want to use fread/fwrite, try using this with gcc, as i explained in a previous mail: __attribute__((packed)) Elias Pschernig |
|
From: Ioannis V. <no...@ya...> - 2001-07-04 12:51:01
|
Now if i understood correctly this is a question for me. Yes a union occupies the space of its biggest member, that's why the minimum size of a struct is a multiple of the sizeof() this union. Of course this was a simplified implementation detail and details in memory management differ in every implementation. That was about my last posting concerning the sizeof a structure. 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 Johannes Pretorius Sent: Wednesday, July 04, 2001 7:08 AM To: dev...@li... Cc: dev...@li... Subject: Fwd: RE: [Dev-C++] [Dev-C++]: Errors in structures >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=20 >memory >space >of the biggest type inside it. for instace in your example the biggest=20 >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=20 >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=20 >file was writen in a diffent program >by a different compiler. You will not read the exact amount of bytes into=20 >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=20 >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=20 >same >structure BUT >DevCpp (or the gcc or mingW) has a different value of the sizeof() so it=20 >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=20 >be >read into the correct value >considering that this is the same (almost ) as doing a memcpy from the=20 >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=20 >>due to compiler's memory management technique. For more information=20 >>read section 8.7 in K&R 2. For example a compiler's memory management=20 >>can be a connected list of structs (compilers, operating systems=20 >>e.t.c. are written in C/C++ too). So in the example of K&R it has a=20 >>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 _______________________________________________ Dev-cpp-users mailing list Dev...@li... http://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
|
From: Ioannis V. <no...@ya...> - 2001-07-04 13:17:27
|
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Johannes
Pretorius
Sent: Wednesday, July 04, 2001 7:08 AM
To: dev...@li...
Cc: dev...@li...
Subject: Fwd: RE: [Dev-C++] [Dev-C++]: Errors in structures
>
>Please for give if I am incorrect but does a union not allocate the=20
>memory
>space
>of the biggest type inside it. for instace in your example the biggest=20
>type is the Align (I think) so the whole
>union will be that size. Note that a union enables varaibles to share a
I had ommitted a line. Also i will put all he comments and say some
lines of the book.
The code is:
typedef long Align /* For allignment att the limits of long */
union header /* The header of the block */
{
struct
{
union header *ptr; /* next block if it is in the list */
unsigned size; /* size of the block
}s;
Align x; /* Imposition of the allignment of blocks */
};
typedef union header Header;
"A free block contains one pointer to the next block of the chain, one
record for the size of the block, and then the free space itself. The
information for control at the beginning, is called "header". For the
allignment to be simplified, all blocks are multiples of the header
size, and the header is properly alligned. This is achieved with one
union which contains the desired structure of the header and one case of
the most restrictive type of alignment, which we have chosen here
arbitrarily as long".
Also it has a graphic showing some stuff.
------------> points at the next free block
|
----------------------------------
| | | |
| | size| |
----------------------------------
|
------------> The address returned to user (in the book
from malloc()).
So the memory used for non built in types are multiples of these blocks.
If anyone knows more i would be happy to learn what he knows....
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|