www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/04/14/20:02:20

Xref: news2.mv.net comp.os.msdos.djgpp:2698
Newsgroups: comp.os.msdos.djgpp
From: sam AT cs DOT vu DOT nl (Megens SA)
Subject: problem with packed structs
Nntp-Posting-Host: flits.cs.vu.nl
Sender: news AT cs DOT vu DOT nl
Organization: Fac. Wiskunde & Informatica, VU, Amsterdam
Date: Sun, 14 Apr 1996 17:08:38 GMT
Message-ID: <Dpv3MF.EwE.0.-s@cs.vu.nl>
Lines: 55
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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>

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

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

- Raw text -


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