0

I am trying to pack a struct into as little space as possible. I have reduced its total memory required to 104 bits, yet sizeof() says that my struct requires a whopping 28 bytes! What have I done wrong?

Next to each item I have indicated what I believe the size is, in bits.

 typedef struct eval {  
    move best; - 56
    uint8_t type; - 8
    int score; - 16
    uint16_t last_access_move; - 16
    int depth; - 8
}

typedef struct move { - 56
    coord from; - 16
    coord to; - 16
    piece captured; - 8
    piece promote_to; - 8
    uint8_t c; - 8
} move;

typedef struct piece { - 8
    char type; - 4
    bool white; - 4
} piece;


typedef struct coord { - 16
    uint8_t col; - 8
    uint8_t row; - 8
} coord;
dylhunn
  • 989
  • 2
  • 8
  • 25
  • The structs contain some internal padding – John Coleman Jul 14 '16 at 00:54
  • or: http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member – pah Jul 14 '16 at 00:55
  • We need to see the definitions of _all_ your custom types for a complete answer (`evaltype` and `castle` are missing) but, your biggest problem is probably that you haven't _told_ the compiler that `int score` only needs 16 bits (for instance). – zwol Jul 14 '16 at 00:55
  • 1
    You've also made a couple of incorrect assumptions. In particular, `char` and `bool` are at least one byte (8 bits), and `int` is usually 32 or 64 bits. –  Jul 14 '16 at 00:57

0 Answers0