Xref: utzoo comp.lang.perl:5228 comp.unix.sysv386:7866 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!dali.cs.montana.edu!caen!hellgate.utah.edu!dog.ee.lbl.gov!nosc!humu!pilikia!art From: art@pilikia.pegasus.com (Art Neilson) Newsgroups: comp.lang.perl,comp.unix.sysv386 Subject: Re: Perl unpack/tacct file Summary: Structure padding Keywords: pack, unpack, tacct files Message-ID: <1991May07.174809.11047@pilikia.pegasus.com> Date: 7 May 91 17:48:09 GMT References: <1991May04.185409.24228@pinhead.pegasus.com> <1991May06.235754.29292@pinhead.pegasus.com> Organization: Pilikia, Honolulu Lines: 40 In article <1991May06.235754.29292@pinhead.pegasus.com> todd@pinhead.pegasus.com (Todd Ogasawara) writes: >I posted a query last week. No responses so far, but I found a >workaround. My question now is, "Why does my workaround work?" > >Here is my original posting. > >In article <1991May04.185409.24228@pinhead.pegasus.com> todd@pinhead.pegasus.com (Todd Ogasawara) writes: >The following Perl program reads tacct type files correctly. My only >problem is I don't understand why it works. I had to place two skips of x2 >in the unpack template (see $stbuf_def) to get the program to work. I'm >pretty sure my problem is not understanding exactly how C structures are >built. I found that a C sizeof(tacctstruct) told me the structure is 52 >bytes large. However, when I added the size of each individual structure >member it added up to 48 bytes. I had to do a hex dump to find that there >were four bytes in two places (two bytes in each place) that never seemed >to be used. Is there some easy rule I can follow when using Perl to unpack >files created using C structures? Your problem most likely is caused by structure padding. The following quote from "C A Reference Manual" Second Edition by Harbison and Steele is quite clear on the subject. "The size of an object of a structure type is the amount of storage necessary to represent all components of that type, including any unused padding space between or after the components. The rule is that the structure will be padded out to the size the type would occupy as an element of an array of such types. (For any type T, including structures, the size of an n-element array of T is the same as the size of T times n.) Another way of saying this is that the structure must terminate on the same alignment boundary on which it started; that is, if the structure must begin on an even byte boundary, it must also end on an even byte boundary." The text in the book goes on for several paragraphs more, providing examples and such. See page 107, section 5.6.7 titled "Sizes of Structures" for the complete story. -- Arthur W. Neilson III | INET: art@pilikia.pegasus.com Bank of Hawaii Tech Support | UUCP: uunet!ucsd!nosc!pilikia!art