www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/08/15/06:24:04

Xref: news-dnh.mv.net comp.os.msdos.djgpp:1545
Path: news-dnh.mv.net!mv!news.sprintlink.net!howland.reston.ans.net!tank.news.pipex.net!pipex!sunsite.doc.ic.ac.uk!ulcc.ac.uk!pluto.ulcc.ac.uk!cziwkga
From: cziwkga AT pluto DOT ulcc DOT ac DOT uk (Kevin Ashley)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Packed structs
Date: 14 Aug 1995 10:52:17 GMT
Organization: University of London Computer Centre
Lines: 31
Distribution: world
References: <DD5MGr DOT 8AE AT jade DOT mv DOT net>
Reply-To: k DOT ashley AT ulcc DOT ac DOT uk
Nntp-Posting-Host: pluto-e.ulcc.ac.uk
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Dj-Gateway: from newsgroup comp.os.msdos.djgpp


In article <DD5MGr DOT 8AE AT jade DOT mv DOT net>, "A.Appleyard" <A DOT APPLEYARD AT fs2 DOT mt DOT umist DOT ac DOT uk> writes:
|>  If I have this struct:-
|>typedef struct{char miaow; int mice_caught,birds_caught; char*name;} cat;
|>  and it needs to be packed, i.e. the fields starting at bytes 0, 1, 5, 9, and
|>the whole struct exactly 13 bytes long with no extra bytes inserted to fill
|>out to a multiple of 2 or 4, should I insert `__attribute__((packed))' after
|>`cat', or before any `;' where djgpp would otherwise be tempted to insert
|>filling bytes, or where?
|>

You should put __attribute__ ((packed)) after each member of the
structure. Don't try to work out which members need it and which
do not ; different releases of gcc may change what they do here,
and if you want to port the code to other platforms the rules
may well be different again (e.g. some systems need 8-byte alignment
rather than 4-byte alignment to get performance gains.)

You don't need to put it after the 'cat'. If you really care about how
big this structure is and you also care about portability, you won't
use 'int', but some other typedef'ed type which guarantees you get
a 4-byte integer. ('int' alone might give you 2, 4 or eight bytes
on systems that are within 20 feet of where I'm writing this ; there
may be other, more exotic possibilities.)

-----------------------------------------------------------------------------
Kevin Ashley                              K DOT Ashley AT Ulcc DOT ac DOT uk
Systems Development Group Manager      http://www.ulcc.ac.uk/staff/Kevin+Ashley
University of London Computer Centre.      ...ukc!ncdlab!K.Ashley
                      This is not a signature

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019