Skip to main content
samiassaad
Associate III
April 6, 2011
Question

Keil and structure pointers

  • April 6, 2011
  • 17 replies
  • 3065 views
Posted on April 06, 2011 at 17:11

Keil and structure pointers

    This topic has been closed for replies.

    17 replies

    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:30

    ''it does not work''

     

    What, exactly, does not work?

    How, exactly, does it fail?

    Do you understand the issues of data alignment - and, hence, why compilers often add padding to structures...?

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    trevor23
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:30

    What exactly ''does not work''? Does it fail to compile or does it fail to do what you expect at run time?

    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:30

    ''it does not do !''

     

     

    How, exactly, do you know that?

    What, exactly, does it do?

    Again:

    Do you understand the issues of data alignment - and, hence, why compilers often add padding to structures...?
    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Tesla DeLorean
    Guru
    May 17, 2011
    Posted on May 17, 2011 at 14:30

    And how exactly are you testing this, and coming to this conclusion?

    int main ()

    {

     /* cast buffer to hdr struct */

     hdr = ( header1*)usart_buffer;

     usart_buffer[0] = 0xE5;

     printf(''%02X\n'',hdr->glcd[0]);

    }

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    samiassaad
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:30

    I expect  from this line is to cast the sizeof(hdr) bytes from the start of usart_buffer to the structure hdr, but it does not do !

    hdr = ( header1*)usart_buffer

    or at least give hdr and usart_buffer the same address location , but it does not !

    what did I miss there ?

    Thank you for your responses .

    samiassaad
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:30

    I checked google for data alignment  nd padding but still confused could you explain please and what is the relation with my problem .

    Thank you all .

    samiassaad
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:30

    I use debuger , so I cant see the right result .

    Andrew Neil
    Super User
    May 17, 2011
    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:30

    Why do you have to make it so hard to wring the information out of you?

    Just saying ''it doesn't work'' or ''it's wrong'' is virtually useless - it's like saying, ''my car doesn't work - what's wrong with it?''

    You need to explain precisely how it's wrong, or how it doesn't work

    ''I cant see the right result''

    So what result, exactly, do you see?

    Have you compared the actual result, and the expected result, and thought about the difference - and how you could explain it?

    Data Aligbnment refers to the requirement that most processors have for data items bigger than 1 byte to start on certain addresses;

    eg, 2-byte objects need to start on an even address, so that they can be fetched in a single operation;

    4-byte objects need to start on a 4-byte boundary, so that they can be fetched in a single operation;

    But 1-byte objects can start on any arbitrary address.

    It should be obvious that this is going to cause issues if you try to mess with pointers where the target objects have different alignment requirements...

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:30

    ''it fact its working''

     

     

    Well, it might just happen not to fail in this case - but you really mustn't make a habit of casting pointers like this!

    If you have a pointer to some unaligned item, and cast it to something that needs to be aligned, then your program will crash.
    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.