This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C6472 Memory Alignment with a BMP Header

Other Parts Discussed in Thread: SYSBIOS

Hi,

 

I am having some trouble generating the header for a BMP file by using the structures below. I set byte to 0x42,0x4D but what I write to filesz gets split between the magic bytes and the upper two bytes of filesz. I am assuming this is an alignment issue with the processor at run time.

 

What's the best way to deal with this?

 

Thanks,

Ian

 

struct bmpfile_magic {

  UINT8 byte[2];

};

struct bmpfile_header {

  UINT32 filesz;

  UINT16 creator1;

  UINT16 creator2;

  UINT32 bmp_offset;

};

struct bmpfile_info

{

UINT32 sz;

UINT32 wid;

UINT32 hei;

UINT16 planes; 

UINT16 bpp; 

UINT32 compression;

UINT32 sz_bmp;

UINT32 hor_rez;

UINT32 vert_rez; 

UINT32 colors_used; 

UINT32 colors_important;

};

  • Hi,

    regarding the control of alignment there are two pragma's available:

    The DATA_ALIGN pragma aligns the symbol in C, or the next symbol declared in C++, to an alignment
    boundary. The alignment boundary is the maximum of the symbol's default alignment value or the value of
    the constant in bytes. The constant must be a power of 2.
    The syntax of the pragma in C is:
    #pragma DATA_ALIGN ( symbol , constant );

    The STRUCT_ALIGN pragma is similar to DATA_ALIGN, but it can be applied to a structure, union type,
    or typedef and is inherited by any symbol created from that type. The STRUCT_ALIGN pragma is
    supported only in C.
    The syntax of the pragma is:
    #pragma STRUCT_ALIGN( type , constant expression );

     

    For more details please see the Compiler User's Guide (SPRU187).

    Kind regards,

    one and zero

  • Ian,

    It is not completely clear to me what you want to make happen. And it is also not clear to me what is actually happening.

    You have declared three structs. All of these structs will be aligned at either 4-byte or 8-byte boundaries, depending on what the compiler enforces or requires. In any case, there will be space between the first and second structs as they are stored in memory.

    I am not really sure how this affects your bmp header or how it fits with your problem description. But maybe it helps you understand why you get the results you are getting.

     

    If this answers your question, please click the  Verify Answer  button below. If not, please reply back with more information.

  • How does this carry over to allocating from a HeapMem instance?

     

     

    Thanks

    Ian

  • Please be more specific.

  •  

    Sorry.

     

    I am using HeapMem_Alloc to allocate memory blocks for the structures. The block is of size: sizeof(struct bmpfile_magic) + sizeof(struct bmpfile_header) + sizeof(struct bmpfile_info) + # of pixels in the image.

     

    Do I need to set the alignment in parameters to the HeapMem instance? If so, how can I do that?

     

    The problem is that I can not have any space between any of the structures in memory... the BMP file format is apparently, very strict.

     

    Thanks.

     

  • Now i know you are using BIOS 6/SYSBIOS and you are allocating a big block from the heap.

    Then what do you do, what are the exact results that you get, and what are the exact results you want to get?

    To learn how to use the arguments of the HeapMem_Alloc call, please see the help docs for SysBIOS, such as heapmem.html. I do not know how to find it from CCS because I have not used SysBIOS, yet.

    To learn how to affect the alignment in your C code with pragmas, please see the C Compiler's User's Guide for the version of the compiler you are using, probably 7.x.

    This thread really belongs in one of the tools forums, so when you get tired of my answers or the direction we are heading, let me know and I will get this thread moved to the BIOS or Compiler forum for you. I do believe that once you explain enough to me, I will be able to help you, though.

  • I ended up just packing the header byte-by-byte. I couldn't figure out a "better" practice.