Path: utzoo!utgpu!water!watmath!atbowler From: atbowler@watmath.waterloo.edu (Alan T. Bowler [SDG]) Newsgroups: comp.lang.c Subject: Re: sizeof( _variable_ ) Message-ID: <20345@watmath.waterloo.edu> Date: 13 Aug 88 01:04:13 GMT References: <1264@bc-cis.UUCP> <1988Jul27.200546.21084@utzoo.uucp> Reply-To: atbowler@watmath.waterloo.edu (Alan T. Bowler [SDG]) Organization: U. of Waterloo, Ontario Lines: 18 In article <1988Jul27.200546.21084@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes: > >Well, yes and no. Sizeof has to include any necessary padding, so that >things like "foovector = (foo *)malloc(n * sizeof(foo))" work properly. >The key word is "necessary". With only char members in the struct, on >most machines there should be no padding needed. Evidently your compiler >is making some worst-case assumptions about structs and is not going to >the trouble of recognizing your struct as an unusually favorable case. >This is perhaps a bit sloppy but is not a violation of specs, since the >specs don't put any constraints on padding. The compiler writer may have good reason for deciding to pad "all char" structs to a word (or double word) boundary. There are a number of machines on which the block move instructions will work much faster if the data is aligned to an appropriate boundary. It may also be that it was just a simplifying assumption that reduces the complexity of the code generator. Simplifying the compiler is no of itself a good thing, but it tends to increase compiler speed, and reduce bugs.