<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: PALLAVI SINGH</title>
    <description>The latest articles on DEV Community by PALLAVI SINGH (@pallavi121).</description>
    <link>https://dev.to/pallavi121</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F626834%2Ff2c776b3-17af-4821-bca7-702d06f859f4.jpg</url>
      <title>DEV Community: PALLAVI SINGH</title>
      <link>https://dev.to/pallavi121</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pallavi121"/>
    <language>en</language>
    <item>
      <title>Structure padding in C- Data Structures</title>
      <dc:creator>PALLAVI SINGH</dc:creator>
      <pubDate>Sun, 22 Aug 2021 14:12:44 +0000</pubDate>
      <link>https://dev.to/pallavi121/structure-padding-in-c-data-structures-3jjj</link>
      <guid>https://dev.to/pallavi121/structure-padding-in-c-data-structures-3jjj</guid>
      <description>&lt;p&gt;In this article we will learn about the structure padding in C in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Let’s recall some basics&lt;/li&gt;
&lt;li&gt;Why structure padding?&lt;/li&gt;
&lt;li&gt;Concept of padding&lt;/li&gt;
&lt;li&gt;How it works?&lt;/li&gt;
&lt;li&gt;More examples to understand better&lt;/li&gt;
&lt;li&gt;How to avoid the structure padding in c?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let’s recall some basics
&lt;/h2&gt;

&lt;p&gt;The memory is assigned/allocated to the members of structure only after the object is declared. Once we declare the object continuous block of memory is allocated to the structure memory.&lt;br&gt;
It will be allocated sequence wise as they are declared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.  struct student
2.  {  
3.     char a;  //1 byte
4.     char b;  //1 byte
5.     int c;  //4 byte
6.  } stud1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why structure padding?
&lt;/h2&gt;

&lt;p&gt;Let’s understand with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.  struct student  
2.  {  
3.     char a;  //1 byte
4.     char b;  //1 byte
5.     int c;  //4 byte
6.  } stud1;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Normally what we do to calculate the size of the structure is add the size of all the data members present in the structure.&lt;br&gt;
So considering the above given size of every data type ,the size of structure object is 6 bytes acc to our general rule.&lt;/p&gt;

&lt;p&gt;But this answer is wrong. Now, we will understand why this answer is wrong? We need to understand the concept of structure padding.&lt;/p&gt;
&lt;h2&gt;
  
  
  Concept of padding
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The processor reads 1 word at a time not 1 byte at a time.&lt;/li&gt;
&lt;li&gt;A 32-bit processor -&amp;gt; 1 word at a time-&amp;gt; 4 bytes-&amp;gt; 1 CPU cycle (1 word=4 bytes).&lt;/li&gt;
&lt;li&gt;A 64-bit processor -&amp;gt; 1 word at a time-&amp;gt; 8 bytes-&amp;gt; 1 CPU cycle (1 word=8 bytes).&lt;/li&gt;
&lt;li&gt;The number of CPU cycles are inversely proportional to performance .&lt;/li&gt;
&lt;li&gt;It means the more number of cycles CPU takes for performing a specific task the lesser will be it’s performance.&lt;/li&gt;
&lt;li&gt;With increase in the number of CPU cycles the performance is decreasing.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_m4bpWCi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/xfsqJKF/memo-alloc-1.png%2520memory%2520allocation" alt="memory allocation"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iHEzmDEm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/3M1YrnQ/box.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iHEzmDEm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/3M1YrnQ/box.png" alt="text box-0"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is evident that it is an unnecessary wastage of CPU cycles. And here the concept of structure padding is introduced.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  How it works?
&lt;/h2&gt;

&lt;p&gt;It is done by the compiler automatically and it saves the no of CPU cycles and hence improves the performance.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6pgT5IYb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/ZcwWTC5/memo-alloc2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6pgT5IYb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/ZcwWTC5/memo-alloc2.png" alt="allocation 2"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W2C6ESnv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/n75TRYb/box1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W2C6ESnv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/n75TRYb/box1.png" alt="text box-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the size of the object is 8 bytes not 6 bytes.&lt;br&gt;
For more clarification let’s discuss the distribution of space:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A occupied=1 byte.//char datatype&lt;/li&gt;
&lt;li&gt;B occupied=1 byte//char datatype&lt;/li&gt;
&lt;li&gt;Vacant rows created occupied=2 bytes C occupied= 4 bytes.//int datatype.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hence the size of the structure object is 1+1+2+4=8 bytes.&lt;br&gt;
So here we have improved the performance but the memory is wasted due to creation of vacant rows.&lt;/p&gt;
&lt;h2&gt;
  
  
  More examples to understand better
&lt;/h2&gt;

&lt;p&gt;Let’s look at another example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.  #include&amp;lt;iostream.h&amp;gt;   
2.  struct student  
3.  {  
4.     char a;  
5.     int b;  
6.    char c;  
7.  };  
8.  int main()  
9.  {  
10.    struct student stud1; // variable declaration of the student type..  
11.    // Displaying the size of the structure student.  
12.    printf("The size of the student structure is %d", sizeof(stud1));  
13.    return 0;  
14. }  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s look at the allocation of memory:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9dEHNvAL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/wyxg6Rt/alloc-3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9dEHNvAL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/wyxg6Rt/alloc-3.png" alt="allocation 3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The total size of the structure object is 4+4+4=12 bytes.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to avoid the structure padding in C?
&lt;/h2&gt;

&lt;p&gt;The structural padding is an in-built process that is automatically done by the compiler. Sometimes we need to avoid it because it increases the size of the structure from it’s actual size.&lt;/p&gt;

&lt;p&gt;We can avoid padding using 2 ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rearranging the attribute.&lt;/li&gt;
&lt;li&gt;Using #pragma pack(1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rearrangement of attributes/variables.&lt;br&gt;
There is one way to reduce the memory wastage manually due to padding.&lt;br&gt;
We can align the data variables in such order that the variable containing more size will be declared first and then the variables having small size should be declared.&lt;/p&gt;

&lt;p&gt;Understand through example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;15. #include&amp;lt;iostream.h&amp;gt;  
16. struct student  
17. {  
18.    Int a;  
19.    char b;  
20.   char c;  
21. };  
22. int main()  
23. {  
24.    struct student stud1; // variable declaration of the student type..  
25.    // Displaying the size of the structure student.  
26.    printf("The size of the student structure is %d", sizeof(stud1));  
27.    return 0;  
28. }  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s look at the allocation:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DovWa88i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/6r19sxB/alloc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DovWa88i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/6r19sxB/alloc4.png" alt="allocation 4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The total size of the struct object is 4+1+1=6 bytes.&lt;/p&gt;

&lt;p&gt;Using #pragma pack(1) directive&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.  #include&amp;lt;iostream.h&amp;gt;   
2.  #pragma pack(1)  
3.  struct base  
4.  {  
5.      int a;  //4 bytes
6.      char b;  //1 byte
7.      double c;//8 bytes  
8.  };  
9.  int main()  
10. {  
11.   struct base var; // variable declaration of type base  
12.   // Displaying the size of the structure base  
13.   printf("The size of the var is : %d", sizeof(var));  
14. return 0;  
15. } 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--klBYIMQz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/pKJw969/alloc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--klBYIMQz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/pKJw969/alloc5.png" alt="allocation 5"&gt;&lt;/a&gt;&lt;br&gt;
Here if we avoid using pragma here then the size of the object structure will be 4+4+4+4=16 bytes.&lt;br&gt;
But the actual size of the structure members is 13 bytes, so 3 bytes are wasted. To avoid the wastage of memory, we use the #pragma pack(1) directive to provide the 1-byte packaging.&lt;/p&gt;

</description>
      <category>structurepadding</category>
      <category>begginers</category>
      <category>concepts</category>
    </item>
    <item>
      <title>HOW TO MAKE ANCHOR LINKS WORK IN MY DEV POST? ANYONE PLEASE HELP.</title>
      <dc:creator>PALLAVI SINGH</dc:creator>
      <pubDate>Sun, 22 Aug 2021 13:42:41 +0000</pubDate>
      <link>https://dev.to/pallavi121/how-to-make-anchor-links-work-in-my-dev-post-anyone-please-help-3346</link>
      <guid>https://dev.to/pallavi121/how-to-make-anchor-links-work-in-my-dev-post-anyone-please-help-3346</guid>
      <description></description>
      <category>questions</category>
      <category>urgent</category>
      <category>help</category>
    </item>
  </channel>
</rss>
