Understanding Structures and Memory Alignment in Programming

Slide Note
Embed
Share

Exploring the concepts of structures in programming, the differences between struct and array, and the size calculation of various structs. Delve into memory retrieval, granularity, and alignment rules for structs, discovering how processors and programmers interact with memory.


Uploaded on Nov 19, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. A Deeper Look at Structure How structures are structured MD. JAKARIA LECTURER DEPT. OF CSE, MIST

  2. Difference between struct and array?

  3. What is the sizeof a struct?

  4. What is the sizeof a struct? Date day month year 4 bytes

  5. What is the sizeof this struct?

  6. What is the sizeof this struct? Time hour minute sec m <unused> 4 bytes

  7. Memory Retrieval https://www.doc.ic.ac.uk/~eedwards/compsys/memory/index.html https://www.ibm.com/developerworks/library/pa-dalign/index.html

  8. Memory Granularity How much information will be read at a time? How programmers see memory How processors see memory https://www.doc.ic.ac.uk/~eedwards/compsys/memory/index.html https://www.ibm.com/developerworks/library/pa-dalign/index.html

  9. What is the sizeof this struct?

  10. What is the sizeof this struct? Time hour minute sec m <unused> day 3 byte padding 4 bytes

  11. Memory Alignment rules for struct - Size of a struct will be divisible by the size of largest member Time hour - Starting address of each member will be divisible by it s size minute sec - char and char[] are special, they can be placed anywhere m <unused> day - Padding is order-dependent 4 bytes

  12. Task: Find out the size of the following structs

  13. Technique to reduce wastage

  14. Technique to reduce wastage 1. Tell the compiler not to pad Time Time hour hour minute minute sec sec m <unused> m day day 4 bytes 4 bytes

  15. Technique to reduce wastage 2. Declare variables in ascending/descending order of size

  16. Technique to reduce wastage 2. Declare variables in ascending/descending order of size struct C c d i struct D c i d

  17. Technique to reduce wastage 3. Use bit fields Each int (if unsigned) can hold = 232 - 1 = 4,29,49,67,295 How many bits should a day require?

  18. Technique to reduce wastage 3. Use bit fields

  19. Technique to reduce wastage 3. Use bit fields Number of bits day should occupy

  20. Technique to reduce wastage 3. Use bit fields What will be the highest value of year? What will be the overall size of struct Day?

  21. Restrictions of Bit Fields - We cannot have pointers to bit field members as they may not start at a byte boundary. - It is implementation defined to assign an out-of-range value to a bit field member. - Bit fields cannot be static in C. - Array of bit fields is not allowed.

More Related Content