www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000305

When Created: 10/15/1999 13:00:04
Against DJGPP version: 2.02
By whom: bandholz@vt.edu
Abstract: Memory footprint for two identical "packed" structures is different.
In an application I was writing I found that defining the same structure two
different ways the memory footprint of the resulting structures was different.
If I used

typedef struct {...} name __attribute__ ((packed));

the resulting structure would be larger than if I did

struct name {...} __attribute__ ((packed));


The latter definition creates the correct output.

Here is a test program that brings out the error.

I compiled this with "gcc -c memory.c" on an NT4 SP5 system.

If you compile to assembly you can see where the "char" variable in test1 is assigned two
bytes of space instead of one.

// memory.c
// Justin Bandholz
// bandholz@vt.edu
// Differences between memory footprints of identical packed structures

#include <stdio.h>
   
typedef struct {
   unsigned char  test1;
   unsigned short test2;
} test1 __attribute__ ((packed));

struct test2 {
   unsigned char  test1;
   unsigned short test2;
} __attribute__ ((packed));

int main(void)
{
   test1 varOne;
   struct test2 varTwo;
   
   varOne.test1 = 0;
   varOne.test2 = 0;
   varTwo.test1 = 0;
   varTwo.test2 = 0;

   printf("size test1:%d size test2:%d\n", sizeof(test1), sizeof(struct test2));
}

Justin Bandholz
bandholz@vt.edu

Note added: 10/18/1999 04:00:42
By whom: eliz@is.elta.co.il
This is a cockpit error: declaring a typedef with
__attribute__((packed)) does NOT pack the struct.  You
need to declare the structure itself as packed.  The
example with the typedef didn't do that; in fact, it
didn't even give the structure a name.

I'm closing this report.

Closed on 10/18/1999 04:00:20: Usage error, not a DJGPP bug
By whom: eliz@is.elta.co.il



  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010