C Structure Alignment Problem + C/C++

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
prajwal
Member
Member
Posts: 154
Joined: Sat Oct 23, 2004 11:00 pm
Contact:

C Structure Alignment Problem + C/C++

Post 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?
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

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

Post 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.
Legend
Member
Member
Posts: 195
Joined: Tue Nov 02, 2004 12:00 am
Contact:

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

Post 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.
*post*
User avatar
prajwal
Member
Member
Posts: 154
Joined: Sat Oct 23, 2004 11:00 pm
Contact:

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

Post by prajwal »

Thanks a lot for all.....
Post Reply