RE: RE: [Dev-C++] [Dev-C++]: Errors in structures
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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 |