From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16748 invoked by alias); 26 Aug 2004 15:59:52 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 16723 invoked from network); 26 Aug 2004 15:59:51 -0000 Received: from unknown (HELO psmtp.com) (12.158.35.213) by sourceware.org with SMTP; 26 Aug 2004 15:59:51 -0000 Received: from source ([192.150.22.8]) by exprod6ob3.obsmtp.com ([12.158.35.250]) with SMTP; Thu, 26 Aug 2004 08:59:48 PDT Received: from inner-relay-1.corp.adobe.com (inner-relay-1 [153.32.1.51]) by smtp-relay-8.adobe.com (8.12.10/8.12.10) with ESMTP id i7QFxSJI015333; Thu, 26 Aug 2004 08:59:33 -0700 (PDT) Received: from iplan-mn (iplan-mn.corp.adobe.com [130.248.25.5]) by inner-relay-1.corp.adobe.com (8.12.9/8.12.9) with ESMTP id i7QFxRTk010513; Thu, 26 Aug 2004 08:59:28 -0700 (PDT) Received: from mn-eljay-a51m.adobe.com ([130.248.178.150]) by iplan-mn.corp.adobe.com (iPlanet Messaging Server 5.2 HotFix 1.21 (built Sep 8 2003)) with ESMTP id <0I3200E3V9R29U@iplan-mn.corp.adobe.com>; Thu, 26 Aug 2004 10:59:27 -0500 (CDT) Date: Thu, 26 Aug 2004 16:19:00 -0000 From: Eljay Love-Jensen Subject: Re: allignment in structures In-reply-to: <4ca029ac04082608234d62cfda@mail.gmail.com> X-Sender: eljay@iplan-mn.corp.adobe.com To: Purnendu/Gmail , gcc-help@gcc.gnu.org Message-id: <6.1.2.0.2.20040826105923.01fbd390@iplan-mn.corp.adobe.com> MIME-version: 1.0 Content-type: text/plain; charset=us-ascii; format=flowed Content-transfer-encoding: 7BIT References: <4ca029ac04082608234d62cfda@mail.gmail.com> X-SW-Source: 2004-08/txt/msg00238.txt.bz2 Hi Purnendu, >A sizeof( struct abc) gives 3, shouldnot i expect it to be 4? No, it should be 3 in this case. The char data type has an alignment of 1. #pragma pack(2) does not increase alignment requirements, it only decreases them. >any pointers??? Use GCC __attribute__ with aligned and pack to affect alignment and/or packing, don't use #pragma pack. // C++ example. #include #include struct Foo { char a __attribute__((aligned(2))); char b __attribute__((aligned(2))); long c __attribute__((packed)); char d; char e; }; int main() { printf("Foo.a %d\n", offsetof(Foo, a)); printf("Foo.b %d\n", offsetof(Foo, b)); printf("Foo.c %d\n", offsetof(Foo, c)); printf("Foo.d %d\n", offsetof(Foo, d)); printf("Foo.e %d\n", offsetof(Foo, e)); } Note: the aligned attribute has certain restrictions, depending on platform. See the online documentation. Often, alignment and packing are used to mimic a canonical data structure, which populates the structure using read or fread. I strongly discourage that practice, and encourage having a helper read routine that populates the structure field-by-field from the byte-by-byte data source. Likewise, the inverse for the write routines. HTH, --Eljay