delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/11/14/01:39:58

Date: Thu, 13 Nov 1997 22:38:30 -0800 (PST)
Message-Id: <199711140638.WAA20781@adit.ap.net>
Mime-Version: 1.0
To: johan AT technologist DOT com (Johan Levin), djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: Struct field aligning

At 06:40  11/13/1997 GMT, Johan Levin wrote:
>struct test_struct {
>	int p1;
>	char ch;
>	int p2;
>};
>[snipped]
>In this case all elements of the structure becomes
>dword-aligned. That might be good for speed, but
>I'm trying to read the header of a file that doesn't
>align it's fields like this. Is it possible to disable
>this aligning for a structure?
Yes. GCC has a field attribute called `packed'. It's in the Info docs; try:
info "gcc" "c extensions" "variable attributes"
Basically, what you want will look something like this:

struct test_struct {
        int p1 __attribute__((packed));
        char ch __attribute__((packed));
        int p2 __attribute__((packed));
};

HTH
 

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


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