Hi. On Fri, May 14, 2004 at 02:46:22PM +0530, Jyotirmoy Das wrote: > Hi Andrea, > Basic reason behind this for the following fact: > Size of a structure may not be always equal to size of its data member > due to memory alignment. Therefore, that is why in the first case, you > are not getting the desired result. However, when you did the read from > file using the individual field, it works. ... and you may use the attribute "packed" to a struct type in order to specify that the minimum required memory be used to represent the type. (see gcc's info pages, Node: Type Attributes) E.g.: typedef struct BMP_H { word ID; dword size; dword res; } __attribute__ ((packed)) BMP_H; should work for your program. PS: Please, don't top-post. See http://www.faqs.org/docs/jargon/T/top-post.html > -----Original Message----- > From: gcc-help-owner@xxxxxxxxxxx [mailto:gcc-help-owner@xxxxxxxxxxx] On > Behalf Of Andrea Pretto > Sent: Friday, May 14, 2004 2:38 PM > To: gcc-help@xxxxxxxxxxx > Subject: Why GCC do this??? > > Hello. > > I'm writing a chunk of software that must read a > bitmap file. > The first field of bitmap header is the ID ( 16 bit ), > and then the size of file ( 32 bit ). > ( after these , thre is a reserved field ( 32 bit )) > > In example. > > 42 4d c6 4a 43 00 00 00 00 00 > > ID is 42 4d , that is BM > Size is 00 43 4a c6, that is 4410045 ( 4.4 mb) > Reserved is 00 00 00 00 ...... nothing. > > I have defined this structure. > > typedef unsigned short word; > typedef unsigned long dword; > > typdef struct BMP_H { > word ID; > dword size; > dword res; > } BMP_H; //this structure is 10 byte > > then... > > fread( (void *)&my, 12 ,1,file ); > > ..don't read the header correct. > It read > > Id: BM ( correct ) > size: 67 ( no ) > res: 3538944 ( no ) > > Looking at file above, I understand that gcc read 4 > byte for Id, in fact 67 ( in hex is 43 ).... > > WHY????????? > Why it read 4 byte instead of 2??? > > In addition sizeof return me 12 instead of 10. > > but, in this way... > > fread( (void *)&my.ID, 2 ,1,file ); > fread( (void *)&my.size, 10 -2, 1, file); > > ..the result is correct.... > > why with the first way don't run correctly?? > > ( i've tried with old TURBO C for msdos, and the > resulty is correct in either ways..) > > Who explane me this, please??? > > > > > > > ____________________________________________________________ > Yahoo! Companion - Scarica gratis la toolbar di Ricerca di Yahoo! > http://companion.yahoo.it -- Claudio