memory alignment within gcc structs

前端 未结 6 518
情歌与酒
情歌与酒 2020-11-29 05:38

I am porting an application to an ARM platform in C, the application also runs on an x86 processor, and must be backward compatible.

I am now having some issues with

6条回答
  •  旧时难觅i
    2020-11-29 06:05

    I've been moving structures back and forth from Linux, Windows, Mac, C, Swift, Assembly, etc.

    The problem is NOT that it can't be done, the problem is that you can't be lazy and must understand your tools.

    I don't see why you can't use:

    typedef struct  
    {  
     unsigned int code;  
     unsigned int length;  
     unsigned int seq;  
     unsigned int request;  
     unsigned char nonce[16];  
     unsigned short  crc;  
    } __attribute__((packed)) CHALLENGE;
    

    You can use it and it doesn't require any special or clever code. I write a LOT of code that communicates to ARM. Structures are what make things work. __attribute__ ((packed)) is my friend.

    The odds of being in a "world of hurt" are nil if you understand what is going on with both.

    Finally, I can't for the life make out how you get 42 or 44. Int is either 4 or 8 bytes (depending on the compiler). That puts the number at either 16+16+2=34 or 32+16+2=50 -- assuming it is truly packed.

    As I say, knowing your tools is part of your problem.

提交回复
热议问题