www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/04/15/11:50:32

Xref: news2.mv.net comp.os.msdos.djgpp:2724
From: avly AT castle DOT net (An)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: problem with packed structs
Date: Mon, 15 Apr 1996 04:11:29 GMT
Organization: The Castle Network, http://www.castle.net
Lines: 70
Message-ID: <4ksieb$l4k@arther.castle.net>
References: <Dpv3MF DOT EwE DOT 0 DOT -s AT cs DOT vu DOT nl>
Reply-To: avly AT castle DOT net
NNTP-Posting-Host: bridge47.castle.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

sam AT cs DOT vu DOT nl (Megens SA) wrote:

>Hi!

>I ran into the problem of aligned fields within a struct. I need to pack a
>struct so it will have exactly the memory-layout I expect it to be (as
>mentioned in the FAQ, section 22.9). I tried the example that was given in
>the info-package about __attribute__((packed)), but although that should
>work, it doesn't...
>A small program I wrote to test it:

><source on>

//
#pragma pack(1)
//

>struct foo
>{
//
    char a __attribute__((packed));
//
>    int x[2] __attribute__((packed));
>};

//
#pragma pack()
//

>int main()
>{
>    foo* f = new foo;

>    printf("size of struct = %d\n", sizeof(foo));
>    printf("f = %p, a = %p, x = %p\n", f, &(f -> a), &(f -> x));
>    return 0;
>}

><source off>

>This produces something like:

>size of struct = 12
>f = 4c0a0, a = 4c0a0, x = 4c0a4

>But what I wanted to see was:

>size of struct = 9
>f = 4c0a0, a = 4c0a0, x = 4c0a1

>To fix this, I could compile with -fpack-struct, but the docs say I
>shouldn't do that if I am using library-defined struct, which is of course
>the case. :) Another solution is to change the layout of the struct (as
>mentioned in the docs), but that also is not an option.

>I'm using DJGPP v2, gcc version 2.7.2 (everything else seems to work fine).

>I read the FAQ, I read the gnu-info docs about packed structs, but I still
>can't get it to work. Does someone know the solution to this?

>Thanks in advance!

>DGreetings,

>SAM

-An



- Raw text -


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