Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!amdcad!ames!umd5!mimsy!oddjob!gargoyle!ihnp4!homxb!mtuxo!rolls!doug!tim From: tim@doug.UUCP (Tim J Ihde) Newsgroups: comp.lang.c Subject: Re: what C needs Message-ID: <496@doug.UUCP> Date: 7 Jan 88 17:34:33 GMT References: <8712241909.AA03710@decwrl.dec.com> <6926@brl-smoke.ARPA> Organization: AT&T ISL - Somerset, NJ Lines: 52 Summary: extra bytes in structures question In article <6926@brl-smoke.ARPA>, gwyn@brl-smoke.ARPA (Doug Gwyn ) writes: > In article <8712241909.AA03710@decwrl.dec.com> devine@cookie.dec.com (Bob Devine) writes: > >When writing code that requires control of alignment or location, > >I find myself using a char array in which I place the members or > >even using unions inside the structure to "force" alignment and > >placement. > > ... > > It would perhaps be of some use on the same system, to conform to > externally-imposed formats, but it isn't usually much harder to > perform I/O for each contiguous piece of the external data than to > try to roll a whole record at a time in or out. Usually this is what one ends up doing; but this raises a question with respect to the new ANSI structure passing (pass-by-value) rules. Suppose I have a structure like: struct { char one[3] ; int two ; } alpha, beta ; On my system there will be at least one extra byte in between the two locations 'one' and 'two', used so 'two' will be properly aligned. Normally I would take great pains to make sure that this extra data does not end up effecting anything, such as being certain to process 'one' and 'two' individually and not 'alpha' as a whole. However, this extra data does get passed along sometimes, such as with C-ISAM where you must do record oriented operations. My question is: as long as I know this byte is there, then I might potentially use it for something. If 'alpha' is a key that a hash algorythm will be used on, the extra byte will be part of the hash result. This would probably have caused so much trouble that I would have defined 'one' to be of length 4 just to get away from this; but IF I HADN'T . . . . What happens to this extra byte when I do beta = alpha ; or foo(alpha) ; ?? Is the extra byte copied along, or is it filled with new-and-improved undefined garbage? In other words, do the new structure operations do memory moves or do they copy each field individually? -- Tim J. Ihde ihnp4!ctsmain!doug!tim (201) 535-9897 Ok, we can all agree that this is my fault.