-1

Correct the alignment of structure below that is bad.

typedef struct{ 
char *string; // 4 byte (type of address int)

char temp; // 1 byte

short pick; // 2 byte

char temp2; // 1 byte

}hello;
  • string = 4
  • temp + pick + temp2(offset 7) = 1+2+1

answer given, good alignment is

char *string; // 4 byte (type of address int)

short pick; // 2 byte

char temp; // 1 byte

char temp2; // 1 byte
  • string = 4
  • pick + temp + temp2(offset 7) = 2+1+1

unable to understand the reason that says temp2 should be at offset 7 rather than 8. how? please help

codey modey
  • 983
  • 2
  • 10
  • 23
  • 3
    which question and which answer are you referring to here? What is the platform you are referring to? char* is 4 bytes (32bit) or 8bytes (64bit) so not sure when you say 2 bytes – Sarang Oct 21 '13 at 05:03
  • just edited the post, ignore the question and answer part, take it to be 32 bit platform.. – codey modey Oct 21 '13 at 05:06
  • it's still wrong: on 32bit platform the pointers are 4 bytes, int is always 4 bytes (irrespective of 32 or 64 bits). Short is 2 bytes and char is 1 byte try reading thru this: http://msdn.microsoft.com/en-us/library/ms253949(v=vs.80).aspx – Sarang Oct 21 '13 at 05:09
  • @Sarang: int can be 64bits on 64bit platforms (ILP64). – Mat Oct 21 '13 at 05:14
  • thanks for the reference, you are right address is int and is 4 byte but still the offset of temp2 remains the same(7) in both cases.. – codey modey Oct 21 '13 at 05:16
  • @Mat: yes for that you will have to use int64_t as defined in stdint.h http://stackoverflow.com/questions/13604137/definition-of-int64-t – Sarang Oct 21 '13 at 05:17
  • @mat, sarang - it is 32 bit platform not 64 bit..is the question in correct..or answer incorrect? – codey modey Oct 21 '13 at 05:27

1 Answers1

0
+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+---+---+---+---+---+---+---+---+
|    string     | pick  | t1| t2|
+---+---+---+---+---+---+---+---+

Using t1 for temp and t2 for temp2, this is the revised layout. The offset of t2 is 7.

For the original structure on a system where n-byte quantities are n-byte aligned, the layout would be:

+---+---+---+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B |
+---+---+---+---+---+---+---+---+---+---+---+---+
|    string     | t1|pad| pick  | t2|pad|pad|pad|
+---+---+---+---+---+---+---+---+---+---+---+---+

That's because the 4-byte pointer needs to be 4-byte aligned, so an array of the structure needs each member to be a multiple of 4 bytes.

Thus, in the original structure, the offset of t2 would have been 8, not 7.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • So, the offset of t2 remains 7 in both question and answer, that means the explanation given in answer to me was wrong. The memory of structure asked in question was already aligned. Thanks! – codey modey Oct 21 '13 at 05:33
  • Thanks Jonathan, I can understand it, but still I'm unable to digest why a short type (i.e. pick) needs to start from an even address, if the memory alignment needs to be 4 bytes. or simply why do I need a pad between t1 and pick? – codey modey Oct 22 '13 at 06:59
  • Because pick is a 2 byte variable so it should start from a 2 byte aligned boundary. It could either have started from slot 4 in pic above. But t1 is there and occupied an even byte. Now if you had a 1 byte variable it could have come in place of 5 (pad), but it is 2 byte, so it should start from an even address. This might help http://stackoverflow.com/a/16456405/986760 – fkl Oct 22 '13 at 07:09
  • @fayyazki, I really appreciate your effort in explaining me the whole idea..but I'm afraid to re-frame my ques that is..if the 32 bit processor is going to read four bytes at a time then how does it make a difference if pick starts from 5th or 6th byte..or in other words, why do you need a 2 byte boundary if 4 byte boundary is already there? and how is 2 byte boundary going to help a 32-bit processor? – codey modey Oct 23 '13 at 05:02
  • It's a question of the instruction set, and where it is easy to load the value into the correct place in the registers. It's not much help if the 2-byte integer is in the middle 2 bytes of a 4-byte register; you'd have to shift it to be able to use it. It's to avoid that sort of thing that chips impose the restrictions. – Jonathan Leffler Oct 23 '13 at 05:26