We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
This seem just dummy question.
What is a problem. I have some old project, that works on 8 bit prosecos. Now I reforming for arm7 procesor.
Ok, I find out, that in old procesor is int 16 bit type, in arm is 32 bit type, no problem. In rtl.h are definitions like U32 as unsigned int, ... and on the and U8 as unsigned char. Sizeof(U8) is 1 - 1 byte (8 bit).
Than i define my own structure for exaple:
typedef struct { U8 byte_1; U16 bytes_2; } MY_STRUCTURE;
sizeof(MY_STRUCTURE) is 4!!! How could be this posible?
Thanke you for your quick reply.
... soo, the sizeof will not work, or can be sizeof somehow change? someone write own code like sizeof that works in this case?
sizeof() will return the true size. It has to include the padding byte because the padding does consume space.
"the sizeof will not work"
Yes, sizeof will work precisely as defined!
The key is a correct understanding of how sizeof works!
A decent 'C' textbook should explain this; See also: c-faq.com/.../endpad.html
I tried like I seid, sizeof(MY_STRUCTURE), but didne't work. I get value 4 not 3! Tested again. This is not such big problem, real problem is that I read data from SD card, for example 3 bytes, but I can't use memcpy(&my_structure, &readed_data[0], 3).
My real structures are 190 bytes big, have 20 subvariables.
Now I using just dummy way. Copy to buffer, than copy to my structure, subvariable by subvariable