From: "Alberto Chessa" Newsgroups: comp.os.msdos.djgpp Subject: Re: Struct with array. HELP Date: 22 Apr 1998 14:50:26 GMT Organization: FIAR S.p.A. Lines: 38 Message-ID: <01bd6dfe$1a58f440$92c809c0@CHESSA> References: <6hi61a$avd1 AT willy DOT cra DOT enel DOT it> Reply-To: "\"Alberto Chessa\" NNTP-Posting-Host: milano22-37.tin.it To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk massarin AT alpha3 DOT cram DOT enel DOT it wrote in article <6hi61a$avd1 AT willy DOT cra DOT enel DOT it>... > My problem is that when I declare a struct like: > > struct a { > > char b[n]; > int c; > . } > > if n (number of char in array) is not a multiple of 4 > the array is enlarged to fit a 4' multiple, > > Does anyone know how to make the struct, and the array, of > the proper size ,apart from doing the struct as an array ? > Is it a bug of compiler ? > > Thanks. > > ps. Please answer by newsgroup . > > It's not a bug, it's an optimization! The processor always read memory at word aligned addresses. Thus, to improve performance, a datum should never begin on odd address. On 486 and Pentium processor, the data should be aligned at 8 byte and procedure at 16 byte (the cache fetch at 0-mod-16 addresses). Two way to pack structures: 1. Use compiler switch "-fpack-struct": A bad solution since You'll pack everithing (degraded performance) 2. Use __attribute__((paked)) (Info gcc.info, node "Variable Attributes"): struct a { char b[n]; int c __attribute__((packed)); }