C Programming and Computer Geeks: C Tutorials.

Search MY Blog

Showing posts with label C Tutorials.. Show all posts
Showing posts with label C Tutorials.. Show all posts

Monday, 27 January 2014

Slack Byte and Structure padding in Structures


What is Slack Byte or structure padding in Structure?

CPU/Computer stores structures using the concept of "word boundary".
What is word boundary?
Word boundary is the size of word(bytes) that CPU can process(read/write) at a time. Usually it is the Accumulator size of a processor(CPU). The size of word boundary is machine dependent.

Slack Byte(Padding byte)
In computer with 4 bytes word boundary, the members of structure are stores left aligned on the wordboundary, as shown below. A character data takes one byte and an integer takes 4 bytes. 3 bytes(Padding bytes) between them is left unoccupied. This unoccupied byte is known as the slack byte.

Structure padding
Structure Padding or Slack bytes


Why Padding or slack bytes are required?
Slack byte used for speed optimization. It aligns bytes so that, that can be read/write from structure faster. It is an extra byte, sometimes placed between structure members to align the structure.
Some Platforms like x86, CPU can access data aligned on absolutely any boundary, not only on "word boundary". The mis-aligned access might be less efficient than aligned access. If data is aligned properly, CPU can access the memory(structure members) efficiently and faster.

Note: As far as CPU is concerned, it can access any data on any boundary.

When we declare structure variables, each one of them may contain the slack bytes and the values stored in such slack bytes are undefined(garbage). Due to this, even though the members of two variable values are equal, their structures do not necessarily to be equal. Therefore C does not permit compassion of structures. However, we need to design our own function that can compare the individual members of structure to decide whether the structures are equal or not.

For understanding more, run the below program and check for memory mapping of the structure variable.

#include "stdio.h"
void main()
{
  struct st_demo
   {
char ch;
int i;
   };

  struct st_demo s;

  printf(" Size of int    = %d \n\n Size of char   = %d \n\n Size of struct = %d ",sizeof(s.i), sizeof(s.ch),sizeof(s));
 
  getch();

}

Let assume the size of char is 1 byte and int is 4 bytes(Variable sizes depends on Platforms/Compilers).
According to this, the size of structure variable s should be 5(1+4) bytes.

Slack byte in structures
Output of the demo program
But from the above programs Output, Size of struct is 8 bytes.

So from where we got this extra 3 bytes?
From the memory map we can observe that there are three bytes between variable is unoccupied. These bytes are called slack bytes.

Note: It is not always necessary that a structure variable contains structure padding or slack bytes. 
If anything needs to be added for the above article, Please post it in comment section.


If you liked the above post then don’t forget to share & comment...


Wednesday, 1 January 2014

Find out the string length without using strlen() function and any loop.


We are having many methods to find the string length for a given string, like implementing the strlen() functionality using loop, etc..

Here in the below program, we are going to see some interesting logic which can give us the string length..

#include "stdlib.h"
#include "stdio.h"

int main()
{

//string length without using strlen() and loop.
     char str[150] = {0,};
     int length = 0;

     printf("Please enter a string:\n");
     gets(str);

     printf("\n\n");

     length = printf("%s", str);
     printf("\n\nLength of entered String is = %d", length);

     getch();
     return 0;
}

Output:

C Program for String length with out using strlen() function and loop
String length by printf()

Logic: We all know that printf() function returns the number of characters displayed(or printed ) on output screen. So using this logic we have found the string length for the entered string.


Read more about basics of Input/output functions.

Hope this Post recalled your basics once again..

Please share it with all your friends.

Post comments for any clarification and doubts. And also share your ideas/opinions as well.

Search This Blog