Page 1 of 1

C Structure Alignment Problem + C/C++

Posted: Sun Aug 07, 2005 11:00 pm
by prajwal
Dear All,

I'm working on a Suse Linux OS on i386 Arch. Using BOCHS Emulator
for developing the hobby OS.

I'v two Questions...

1) QUERY 1?

if a declare a structure as
struct IDTR
{
unsigned short limit ; //2 bytes
unsigned base ;//4 bytes
} ;

I See that limit is allocated address an 0x100 (say) then
base is alligned at 0x104 when is is suppose to be at 0x102.

The gcc compiler which i'm using is aligning at int boundaries
and i didn't find any gcc option to avoid this for i386 Arch (But
-mno-align-double is supported). But for other Arch like Power PC,
Etc... -mno-align-int is available...

Is there a way to force GCC not to align on int boundaries.....

2) QUERY 2?

What are the Advatages/DisAdvantages of using C for developing an OS kernel?

What are the Advatages/DisAdvantages of using C++ for developing an OS kernel?

Re: C Structure Alignment Problem + C/C++

Posted: Sun Aug 07, 2005 11:00 pm
by carbonBased
Query 1:

All good optimizing compilers will do this, as there is a performance penalty to accessing non-aligned memory. Some processors cannot even do this, in fact, and will cause an exception and/or reset.

In GCC, to remove alignment padding, add a packed attribute:

struct IDTR
{
unsigned short limit ; //2 bytes
unsigned base ;//4 bytes
} __attribute__ ((packed));

This is gcc only, of course... and will make your code non-portable, therefore I'd suggest something like:

#ifdef __GCC__
#define PACKED __attribute__ ((packed))
#else
#define PACKED
#endif

Query 2:

This has been answered several times in the past. I'd suggesting looking at the history of this forum.

Re: C Structure Alignment Problem + C/C++

Posted: Sun Aug 07, 2005 11:00 pm
by Legend
C++ does require some more runtime support, C does not really need additional functions for some language constructs.

C++ gives you classes, inheritance, etc., C obviously does not.

Re: C Structure Alignment Problem + C/C++

Posted: Mon Aug 08, 2005 11:00 pm
by prajwal
Thanks a lot for all.....