1

I compiled this code:

// Example program
#include <iostream>
using namespace std;
struct A
{
    char a;
};
struct B
{
    char b;
    int a;
};
struct C
{
    int * a;
    unsigned char b;
};

int main()
{
    cout<< "size of Strcut A:\t"<< sizeof(A)<<endl;
    cout<< "size of Strcut B:\t"<< sizeof(B)<<endl;
    cout<< "size of Strcut C:\t"<< sizeof(C)<<endl;
    cout<< "size of int* :  \t"<< sizeof(int*)<<endl;
    return 0;
}

And I got this result:

size of Strcut A:   1
size of Strcut B:   8
size of Strcut C:   16
size of int* :      8

now I want to ask why the size of Strcut B is not 5? why the size of Struct C is not 9? when the memory is importent in Embedded system how I should save memory in another platforms like ARM?

Can I say to the compiler it's 5 bytes or 9 bytes?

http://cpp.sh/2rv6b

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Prof. Hell
  • 729
  • 12
  • 19
  • 1
    You might want to look up the term "struct padding." Once you have that term, you can probably find a lot of really good resources about it on this site. Hope this helps! – templatetypedef Aug 24 '16 at 23:44
  • 1
    You're on a system with 64-bit pointers, so telling it that the struct is smaller (5 bytes) than the pointer (8 bytes) might be a bad idea. – kfsone Aug 24 '16 at 23:45
  • 1
    There might be a compiler-specific way to specify that you want the structure to be packed -- but [that can be unsafe](http://stackoverflow.com/q/8568432/827263). Note that the code to access members of a packed structure is likely to be larger, so packing doesn't help unless you have a lot of objects of the same type (or possibly if code and data memory are allocated independently). – Keith Thompson Aug 24 '16 at 23:48
  • but I need memory efficient in AVR or ARM platform! – Prof. Hell Aug 24 '16 at 23:51
  • 1
    With `gcc`, do (e.g.) `struct A __attribute__((packed)) {`, but then you _might_ get an alignment exception [not from the compiler, but from the processor at runtime], so try it, but be careful – Craig Estey Aug 24 '16 at 23:56
  • thanks @CraigEstey – Prof. Hell Aug 24 '16 at 23:58
  • @CraigEstey , Is __attribute__((packed)) need some header file? because it give me this error : error: expected unqualified-id before '{' token – Prof. Hell Aug 25 '16 at 00:03
  • 1
    Oops. My bad. I did that from memory. It was misplaced. I just checked some of my actual code. It is `struct A { char x; int *y; } __attribute__((packed));`. It can even be `typedef struct A { char x; int *y; } __attribute__((packed)) A_t;` – Craig Estey Aug 25 '16 at 00:08
  • 1
    @Prof.Hell Be careful, you may get a core dump for misaligned data on ARM. – fefe Aug 25 '16 at 01:47

2 Answers2

1

Alignment. The members of your datastructures (and their total sizes) are padded with empty space between in order to speed up access and reduce redundant reads that would be necessary when a larger type spans a boundary.

Taywee
  • 1,313
  • 11
  • 17
  • but I need memory efficient in AVR or ARM platform! – Prof. Hell Aug 24 '16 at 23:50
  • 1
    Then use compiler flags to force a packed struct. More preferably, just don't worry about it until it actually blocks you. If you're worried about memory limitations, there are tons of other things you should check before trying to eliminate struct padding. I wouldn't start trying to fix a problem you aren't having yet. – Taywee Aug 24 '16 at 23:55
1

The compiler decides to add some extra padding bits to allign your struct. It's much faster to work with power of 8 data then spending time to extract them from memory.

Ouss4
  • 479
  • 4
  • 11