data alignment issue

data alignment issue

Post by Viktor.Stuj.. » Fri, 04 Jul 2003 21:04:47



I've noticed that djgpp aligns all members of a structure to 4 bytes.
---------------------------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
----------------------------------------------------
| a   _   _   _   b   b   b   b   c    c    _    _ |
----------------------------------------------------

this is the layout of this structure in memory:
struct {
 BYTE a;
 DWORD b;
 WORD c;

Quote:}

because of the 4byte alignment, sizeof() returns 12 instead of 7.
Imagine the mess that it causes in this example:
 STR str; void *ptr;
 ptr=&str;
 ptr=malloc...; fread(ptr, 7, 1, file);
guess what (STR *)ptr->a would print...
that's right, the data from the first byte and the next 3 bytes together!
the member a is automatically treated as a DWORD.
Can someone tell me a:
1) compiler switch
2) keyword
3) method
with which I could correct the error?

 
 
 

data alignment issue

Post by Hans-Bernhard Broeke » Fri, 04 Jul 2003 21:48:50



> Can someone tell me a:
> 1) compiler switch
> 2) keyword
> 3) method
> with which I could correct the error?

No.  Because there is no error in DJGPP's behaviour, in the first
place, thus nothing to be corrected.  The assumptions hard-coded into
the program snippets you showed are where the real error is.  These
assumption simply don't hold, and code making such assumptions is
broken.

While there are indeed methods to make some structs' layouts fit your
expectations, using them is a) unportable, and b) will usually fail on
at least some platforms such code will have to run on, anyway.

For more background on this, please consult the C FAQ, posted
regularly to the comp.lang.c and comp.lang.c.moderated newsgroups,
among other places.

No. You write here --> you read here.
--

Even if all the snow were burnt, ashes would remain.

 
 
 

data alignment issue

Post by Gautier Write-onl » Sat, 05 Jul 2003 08:52:22



Quote:> I've noticed that djgpp aligns all members of a structure to 4 bytes.

Not necessarily (see below).

Quote:> ---------------------------------------------------------------------
> | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
> ----------------------------------------------------
> | a   _   _   _   b   b   b   b   c    c    _    _ |
> ----------------------------------------------------

> this is the layout of this structure in memory:
> struct {
>  BYTE a;
>  DWORD b;
>  WORD c;
> }

with Interfaces;                use Interfaces;
with Ada.Integer_Text_IO;       use Ada.Integer_Text_IO;

procedure Align is

  type r1 is record
    a: Unsigned_8;  -- 1
    b: Unsigned_32; -- 4
    c: Unsigned_16; -- 2
  end record;

  type r2 is record
    a: Unsigned_8;  -- 1
    b: Unsigned_32; -- 4
    c: Unsigned_16; -- 2
  end record;
  pragma pack(r2);

begin
  Put(r1'size/8); Put(r2'size/8);
end;

DJGPP takes 10 bytes for r1 (choice) and 7 for r2 (it must,
because of "pack").
________________________________________________________
Gautier  --  http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!

 
 
 

data alignment issue

Post by Andre » Sun, 06 Jul 2003 11:27:14




Quote:>I've noticed that djgpp aligns all members of a structure to 4 bytes.
>---------------------------------------------------------------------
>| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
>----------------------------------------------------
>| a   _   _   _   b   b   b   b   c    c    _    _ |
>----------------------------------------------------

>this is the layout of this structure in memory:
>struct {
> BYTE a;
> DWORD b;
> WORD c;
>}

>because of the 4byte alignment, sizeof() returns 12 instead of 7.

This is normal with all modern 32 bit compilers due to optimizations
in the compiler becuase the CPU loads data at a 4 byte boundary allot
faster that at non 4 byte boundaries.

Have a look in the faq\DJGPPFAQ.TXT file for pack. There are a two
different ways of coding it so that the struct is packed, but be aware
that you will pay a speed penalty for packing the structure.

See setion 18.5 & 22.11 & 22.12 for examples of the two options you
can use. The #pragma optino is portable to some other compilers such
as Borland.

Andrew

 
 
 

1. Again: data alignment

I have a table:

unsigned int pgdir[1024];

It is put by compiler in .bss section. This table should be page
(4096-bytes) aligned.

This is my linker script:

OUTPUT_FORMAT("coff-go32")
SECTIONS
{
  .main 0x100000:
  {
    *(.header)
    *(.data)
    *(.text)
  }
  .bss ALIGN(4096):
  {
    *(.bss)
  }

How should I define the alignment?

2. Partition Magic Won't change size of Extended partition

3. Data Alignment for Optimal Access

4. NT4 PDC and Windows 2000 AD Security Question

5. data alignment

6. Regarding isosurface

7. Problem with data alignments...

8. Will I regret buying Lexar 256 mb sd card for IPAQ h1910?

9. Question about Djgpp : Data Alignment

10. Automatic Data Segment Exceeds 64K In Program With No Data?

11. alignment

12. Alignment problem

13. Alignment fault signals