-1

I am a bit lost on calculating the size of structures

So we have the structure:

struct AcronymNode{ 
    struct AcronymNode* next; 
    char acronym[5]; 
    double num_phrases; 
    struct The_Phrase* phrase_list; 
    } Dictionary;

I see it as

next : 4bytes
acronym: 5bytes + 3
num_phrases: 8bytes
phraselist: 4bytes
=24 bytes

When I look at the notes it says: 32 bytes = 4 + 5 + 3 (alignment to word) + 4 (to align for the double) + 8 + 4 + 4 (to align next structure to a multiple of 8 for the double)

Why are we adding an extra 8 for alignment since it doesn't overflow, 4 before the double and 4 after the 2nd structure

In the more efficient structure it has double first, following the structures for 24 bytes

Also I wanted to check if this is right

structT{
    int a;
    char b[5];
    float c;
    char d[2];
    };

Is the size 4 + 5+3 + 4 + 4 = 20?

HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
user3543440
  • 45
  • 1
  • 9
  • 1
    Alignment is added for performance reasons. If you don't want it, use __attribute__((packed)) (gcc/clang). Also, compiler is not going to reorder fields for you, it's your responsibility. – oakad Apr 17 '14 at 03:21
  • This wikipedia page is a good source of information on why padding is added : http://en.wikipedia.org/wiki/Data_structure_alignment. There are nice examples on x86 architecture. – slaadvak Apr 17 '14 at 03:24
  • This is caused by the compiler trying to align object to match address boundaries that are appropriate for a type. Take a look at [Memory alignment in C-structs](http://stackoverflow.com/questions/5435841/memory-alignment-in-c-structs). – R Sahu Apr 17 '14 at 03:24
  • What notes are you talking about? Give the link. – HelloWorld123456789 Apr 17 '14 at 03:30
  • @Barmar this is from a sample exam – user3543440 Apr 17 '14 at 03:39
  • So? I still think the answer in the other question will explain what you need to know. – Barmar Apr 17 '14 at 03:42

2 Answers2

0

packing structs to a 1 byte boundary is used to optimize for space, otherwise the compiler will pad out for speed performance.

See: http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding

cwhelms
  • 1,731
  • 3
  • 12
  • 14
0

If memory access latency is not something you are concerned with, you can instruct the compiler to layout a structure on a different alignment (than that which is most efficient for the machine). For example:

#pragma pack(1)
struct AcronymNode{ 
    struct AcronymNode* next; 
    char acronym[5]; 
    double num_phrases; 
    struct The_Phrase* phrase_list; 
    } Dictionary;
#pragma pack()

Although '#pragma pack' is not officially part of the C language, it is supported by most compilers. In the example above, '#pragma pack(1)' instructs the compiler to pack the structure on a one-byte boundaries, effectively changing the layout of the structure as you described it:

next : 4bytes
acronym: 5bytes + 3
num_phrases: 8bytes
phraselist: 4bytes
=24 bytes

Then, '#pragma pack()' returns alignment back to it's default.

.

'#pragma pack(1)' is often used to define structures where other alignments are not desirable; for example, when sending such structures "over the wire" to another system. Wire protocols are generally packed so that there is no filler between fields.

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28