public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: structure packing.
@ 2008-10-20  7:52 Duft Markus
  0 siblings, 0 replies; 8+ messages in thread
From: Duft Markus @ 2008-10-20  7:52 UTC (permalink / raw)
  To: gcc-help

> 
> Hi!
> 
> I have a strange problem with some of my source code. I have something
> similar as the small test case below, which I compile with multiple
> different compilers (and compiler versions), including visual c++
2003,
> 2005 and 2008 and GCC 3.3 and 4.2.
> 
> It seems that gcc 4.2 makes some troubles ;) could please somebody
take
> a look at the things below, and tell me wether I'm just blind and
dumb,
> or there is a problem with gcc?

Some more testing revealed that using the gcc_struct attribute
additionally, makes it work.... is there any explanation for this? I
thought gcc_struct would be the default? (btw. I _am_ on windows, but
using interix, which I think should be pretty much unix, and nearly no
windows...).

Cheers, Markus

> 
> Thanks in advance, Cheers, Markus
> 
> mduft build $ cat zz.c
> #include <stdio.h>
> 
> typedef struct __attribute__((packed, aligned(2))) {
> 	unsigned int a;
> 	unsigned short b;
> 	unsigned int c;
> } teststruct;
> 
> int main(void) {
> 	printf("uint: %d, ushort: %d, struct: %d (should be %d)\n",
> 		sizeof(unsigned int), sizeof(unsigned short),
> sizeof(teststruct),
> 		(sizeof(unsigned int) * 2) + sizeof(unsigned short));
> }
> mduft build $ gcc zz.c
> mduft build $ ./a.out
> uint: 4, ushort: 2, struct: 12 (should be 10)
> 
> mduft build $ gcc -v
> Using built-in specs.
> Target: i586-pc-interix6.0
> Configured with: /opt/gentoo.system/var/tmp/portage/sys-devel/gcc-
> 4.2.4-r00.1/work/gcc-4.2.4/configure --prefix=/opt/gentoo.system/usr
--
> bindir=/opt/gentoo.system/usr/i586-pc-interix6.0/gcc-bin/4.2.4 --
> includedir=/opt/gentoo.system/usr/lib/gcc/i586-pc-
> interix6.0/4.2.4/include --datadir=/opt/gentoo.system/usr/share/gcc-
> data/i586-pc-interix6.0/4.2.4 --
> mandir=/opt/gentoo.system/usr/share/gcc-data/i586-pc-
> interix6.0/4.2.4/man --infodir=/opt/gentoo.system/usr/share/gcc-
> data/i586-pc-interix6.0/4.2.4/info --with-gxx-include-
> dir=/opt/gentoo.system/usr/lib/gcc/i586-pc-
> interix6.0/4.2.4/include/g++-v4 --host=i586-pc-interix6.0
--build=i586-
> pc-interix6.0 --disable-altivec --disable-nls --with-system-zlib --
> disable-checking --disable-werror --enable-secureplt
--disable-multilib
> --disable-libmudflap --disable-libssp --disable-libgcj --enable-
> languages=c,c++,treelang --enable-shared --enable-threads=posix
--with-
> local-prefix=/opt/gentoo.system/usr
> Thread model: posix
> gcc version 4.2.4 (Gentoo 4.2.4-r00.1 p1.0)
> 
> mduft build $ /opt/gcc.3.3/bin/gcc zz.c
> mduft build $ ./a.out
> uint: 4, ushort: 2, struct: 10 (should be 10)
> mduft build $

^ permalink raw reply	[flat|nested] 8+ messages in thread
* RE: structure packing
@ 2014-01-16 10:45 vijay nag
  2014-01-16 11:34 ` David Brown
  0 siblings, 1 reply; 8+ messages in thread
From: vijay nag @ 2014-01-16 10:45 UTC (permalink / raw)
  To: gcc-help

Dear gcc,

Why is the size of below struct UnPackedStruct 6 ? Is there any switch
similar to -E in gcc to check how data alignment is performed by GCC
for structs ?


#include <stdio.h>
#include <stdint.h>

struct tlv_header_t {
    unsigned char tlv_length_upper : 3;
    unsigned char reserved : 5;
    unsigned char tlv_length_lower;
    uint16_t tlv_type;
};

typedef struct {
  struct tlv_header_t  sess;
  uint8_t p;
} UnPackedStruct;

typedef struct {
   int a;
   char b;
} PaddedStruct;

int main()
{
  UnPackedStruct a;
  PaddedStruct b;

  printf("size of a = %u\n", sizeof(a));
  printf("size of b = %u\n", sizeof(b));

  return 1;
}

./padding
size of a = 6
size of b = 8

^ permalink raw reply	[flat|nested] 8+ messages in thread
[parent not found: <ba0bd44d0905210723n5e6e85f5rea8b2b52c381d5d9@mail.gmail.com>]
* structure packing.
@ 2008-10-20  7:33 Duft Markus
  0 siblings, 0 replies; 8+ messages in thread
From: Duft Markus @ 2008-10-20  7:33 UTC (permalink / raw)
  To: gcc-help

Hi!

I have a strange problem with some of my source code. I have something
similar as the small test case below, which I compile with multiple
different compilers (and compiler versions), including visual c++ 2003,
2005 and 2008 and GCC 3.3 and 4.2.

It seems that gcc 4.2 makes some troubles ;) could please somebody take
a look at the things below, and tell me wether I'm just blind and dumb,
or there is a problem with gcc?

Thanks in advance, Cheers, Markus

mduft build $ cat zz.c
#include <stdio.h>

typedef struct __attribute__((packed, aligned(2))) {
	unsigned int a;
	unsigned short b;
	unsigned int c;
} teststruct;

int main(void) {
	printf("uint: %d, ushort: %d, struct: %d (should be %d)\n",
		sizeof(unsigned int), sizeof(unsigned short),
sizeof(teststruct),
		(sizeof(unsigned int) * 2) + sizeof(unsigned short));
}
mduft build $ gcc zz.c 
mduft build $ ./a.out 
uint: 4, ushort: 2, struct: 12 (should be 10)

mduft build $ gcc -v
Using built-in specs.
Target: i586-pc-interix6.0
Configured with:
/opt/gentoo.system/var/tmp/portage/sys-devel/gcc-4.2.4-r00.1/work/gcc-4.
2.4/configure --prefix=/opt/gentoo.system/usr
--bindir=/opt/gentoo.system/usr/i586-pc-interix6.0/gcc-bin/4.2.4
--includedir=/opt/gentoo.system/usr/lib/gcc/i586-pc-interix6.0/4.2.4/inc
lude
--datadir=/opt/gentoo.system/usr/share/gcc-data/i586-pc-interix6.0/4.2.4
--mandir=/opt/gentoo.system/usr/share/gcc-data/i586-pc-interix6.0/4.2.4/
man
--infodir=/opt/gentoo.system/usr/share/gcc-data/i586-pc-interix6.0/4.2.4
/info
--with-gxx-include-dir=/opt/gentoo.system/usr/lib/gcc/i586-pc-interix6.0
/4.2.4/include/g++-v4 --host=i586-pc-interix6.0
--build=i586-pc-interix6.0 --disable-altivec --disable-nls
--with-system-zlib --disable-checking --disable-werror
--enable-secureplt --disable-multilib --disable-libmudflap
--disable-libssp --disable-libgcj --enable-languages=c,c++,treelang
--enable-shared --enable-threads=posix
--with-local-prefix=/opt/gentoo.system/usr
Thread model: posix
gcc version 4.2.4 (Gentoo 4.2.4-r00.1 p1.0)

mduft build $ /opt/gcc.3.3/bin/gcc zz.c 
mduft build $ ./a.out 
uint: 4, ushort: 2, struct: 10 (should be 10)
mduft build $

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-01-16 11:34 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-20  7:52 structure packing Duft Markus
  -- strict thread matches above, loose matches on Subject: below --
2014-01-16 10:45 vijay nag
2014-01-16 11:34 ` David Brown
     [not found] <ba0bd44d0905210723n5e6e85f5rea8b2b52c381d5d9@mail.gmail.com>
2009-05-21 14:39 ` Mohamed Shafi
2009-05-22  6:39   ` Ian Lance Taylor
2009-05-22  8:01     ` Mohamed Shafi
2009-05-22 13:51       ` Ian Lance Taylor
2008-10-20  7:33 Duft Markus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).