1

I have a program that reads a bin, and stores data in an array of structs.

My problem is that all the variables of the struct (int, long, unsigned int, char[8] etc) counted manually should add up to 60. A provided .bin file with only 1 entry (which is confirmed to be correct), also counts as 60 bytes using the following code:

fseek(file, 0, SEEK_END);
long int length = ftell(file);
fseek(record, 0, SEEK_SET);

HOWEVER, when I'm assigning space, I'm using a printf statement to debug:

struct Data *entries = malloc(length); // length is 60 from above code
printf("%d entries of size %ld", count, sizeof(struct Data)); // count is number of entries

// RESULT IN CONSOLE IS "1 entries of size 80"

So obviously, there's an extra 20 bytes when using sizeof(x). Why?

Also if that's the case, how DO I go about allocating the appropriate space, reading from a .bin file, if I don't know how many entries there are?

Thanks!

EDIT: This is the struct (mine):

struct Data{
    unsigned int creator;
    unsigned short fish;
    short vacation;
    char existence[8];
    short front;
    int bait;
    short peace;
    char night;
    char burst;
    unsigned long snow; 
    char finger;
    double idea;
    float goodbye;
    int stocking;
    char bell;
    double grandfather;
}
Astantos
  • 117
  • 1
  • 12
  • 3
    The obvious answer is alignment of elements (int to 4 bytes, double to 8 bytes, etc.) which we don't know without you disclosing 'struct Data'. – Aki Suihkonen Aug 26 '17 at 05:23
  • 2
    In other words, the compiler adds padding to the struct fields to optimize memory accesses, making the actual struct footprint bigger than expected. – DYZ Aug 26 '17 at 05:24
  • Ok thanks, that explains a lot. But I was reading this example, and just wanted to clarify, WHEN does the padding actually occur? In the example I was looking at, according to the answer, struct Y goes 4, 1, PAD 1 then 2? By my understanding I thought it'd be something like, 4, 1, 2, PAD 1 . . . https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member – Astantos Aug 26 '17 at 05:31
  • use `__attribute__((packed))` to ensure that gcc doesn't align the structure members. – Sajad Banooie Aug 26 '17 at 05:31
  • If you showed us the struct we could do more than guess where the padding might be. – Retired Ninja Aug 26 '17 at 05:32
  • Oh yeah, sure my bad, I'll edit the post and put it up – Astantos Aug 26 '17 at 05:33
  • Something like this can help you see where the padding is. I used your struct, then optimized your struct to avoid padding where possible. You can compare each against the packed version as well. https://ideone.com/u8lkpk – Retired Ninja Aug 26 '17 at 06:29
  • 1
    @DYZ It is not only optimalisation. On some architectures some types is not only "unoptimal" but impossible operate from un aligned address – Jacek Cz Aug 26 '17 at 08:29
  • @JacekCz most modern architectures forbid unaligned access. You can even do that on x86 if you enable some appropriate flags – phuclv Aug 28 '17 at 04:33

1 Answers1

1

The compiler adds padding bytes (They are never used when accessing or writing to structure) to make sur data is correctly aligned for performance.

Karim Manaouil
  • 1,177
  • 10
  • 24