Thread (10 messages) flat view 10 messages, 4 authors, 2016-08-11

Re: sizeof(struct ...)

From: Erik Mouw <hidden>
Date: 2016-08-11 20:09:51

On Thu, Nov 23, 2006 at 04:45:09PM +0100, Ren? Scharfe wrote:
Andy Whitcroft schrieb:
quoted
You see packing attributes applied to similar things in the kernel.
Perhaps they are relevant here?
Is there not some kind of attribute thing we can apply to this structure
to prevent the padding?  You see that in the kernel from time to time.

struct foo {
} __attribute__((packed));
Yes, that would be nice, but unfortunately __attribute__ is no standard
C.
There is no standard C way to pack structures. Some compilers use
#pragma's, gcc uses __attribute__((packed)).
 Is there really a compiler that inserts padding between arrays of
unsigned chars?
Yes, that compiler is called "gcc".

#include <stdio.h>

struct foo {
        unsigned char a[3];
        unsigned char b[3];
};

int main(void)
{
        printf("%d\n", sizeof(struct foo));
        return 0;
}

On i386 that prints 6, on ARM it prints 8.


Erik

-- 
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help