cancel
Showing results for 
Search instead for 
Did you mean: 

sizeof() function

matic
Associate III
Posted on April 08, 2016 at 21:21

Hello.

I would like to get a size of a struct in bytes. Members of a struct are different - concretely I have 6 WORDS and 1 array of 6 BYTES. Then, the size of a struct would be 30 BYTES. If I use function sizeof(struct), it returns 32, which obviously counts array as 8 BYTES  (2 WORDS).

Is there any other option how to get the proper size in bytes? I use Keil MDK, if it depends on that.

Thank you in advance.
7 REPLIES 7
Radosław
Senior
Posted on April 08, 2016 at 22:06

Bytes probably aren't at end of structure, so this is correct behavior.

Posted on April 08, 2016 at 23:09

Structures tend to pack in an architecture compatible manner, this avoids alignment issues. sizeof() will reflect the memory footprint of the structure, and would be the size you need to malloc() or fwrite() to interact with it.

There are many occasions, when dealing with file systems or serial protocols, where byte level packing are required.

This is usual compiler specific, but often takes the form of

#pragma pack(1)

 ..

#pragma pack

http://www.keil.com/support/man/docs/ARMCC/armcc_chr1359124990875.htm

This does cause headache when unaligned reads/writes are not supported. Watch especially Cortex-M0, or LDRD of doubles. Here you'll need to memcpy() elements to suitably aligned memory.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mark239955_stm1
Associate II
Posted on April 09, 2016 at 03:35

Further to clive's comments, adding the appropriate attribute to the struct declaration may be appropriate, e.g.

typedef struct __attribute__((packed)) _mystruct {

int fields[6];

char data[6];

} mystruct;

will cause sizeof(mystruct) to return 30.

The __attribute__((packed)) syntax applies to GCC; my recollection is that it's slightly different under Keil, but the principle is the same.
matic
Associate III
Posted on April 09, 2016 at 07:44

Thank you, all.

One more thing. I read, that access to packed structure is slower, because it has to access byte by byte, if  am correct. But do this apply only when accessing this packed struct or all of the data in project (assuming that only one struct is declared as packed)?

Radosław
Senior
Posted on April 09, 2016 at 12:32

Not byte by byte. but you get not aligned access, then core on M0(+) this will give you hardfault, on other cores this will be slower beose core combines data with to aligned reads. 

Posted on April 09, 2016 at 15:30

While it is possible to create chronic misalignment of your entire data space, you'd have to try really hard. People have done stupider things in the past, see the prevelant stack alignment fail in a lot of GNU/GCC linker scripts.

The alignment will be contained within the structure. The RAM width is 32-bits, a misaligned read will require 2 memory cycles, a misaligned write 4 if byte lanes are not supported.

Still, you'd want to be really wary about passing pointers, the double/LDRD situation can catch you out as the compiler might make the assumption your pointers are always aligned.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Radosław
Senior
Posted on April 09, 2016 at 17:10

>>see the prevelant stack alignment fail in a lot of GNU/GCC linker scripts.

ST get 1 place in this