This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how would be a char / short variable stored?

If I've got a struct like:

struct mystruct {
unsigned char   first;
unsigned short  second;
};

How would this be stored in the memory if I'm working with a 32Bit ARM7 microcontroller (SAM serie from ATMEL)? for example first=1 and second=2 in decimal.

When I look into the memory window would it looks like
0x20000000 01 00 00 00
0x20000004 10 00 00 00

(so that a char variable needs the same space like a int variable)

0x20000000 01 10 00 00
(or does it looks like that one)? So that the char variable only needs two bytes and the short variable gets the rest of the 8 bytes?

best regards and thanks for your replies
Mark

  • struct mystruct {
    unsigned char   first;
    unsigned short  second;
    };
    

    0x20000000 01 NA 10 00

    The char would go at 0x20000000
    The short would go at 0x20000002

    0x20000001 would be a "wasted" byte

    packing the structure would cause the short to be stored starting at 0x20000001

    The algignment of the next variable defined (outside of the structure) would be defined by that variables alightment rules.

  • How would this be stored in the memory if [...]?

    Ultimately, that's none of your business to know. The program will have two variables that can be accessed individually, or as a group. All else is up to the compiler, and should be kept that way.