SlideShare a Scribd company logo
1 of 7
Download to read offline
Topics
Bit Fields
enum
union
C - Bit Fields
Suppose your C program contains a number of TRUE/FALSE variables grouped in a
structure called status, as follows −
struct {
unsigned int widthValidated;
unsigned int heightValidated;
} status;
This structure requires 8 bytes of memory space but in actual, we are going to store
either 0 or 1 in each of the variables. The C programming language offers a better
way to utilize the memory space in such situations.
If you are using such variables inside a structure then you can define the width of a
variable which tells the C compiler that you are going to use only those number of
bytes. For example, the above structure can be re-written as follows −
struct {
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status;
The above structure requires 4 bytes of memory space for status variable, but only 2
bits will be used to store the values.
If you will use up to 32 variables each one with a width of 1 bit, then also the status
structure will use 4 bytes. However as soon as you have 33 variables, it will allocate
the next slot of the memory and it will start using 8 bytes. Let us check the following
example to understand the concept −
#include <stdio.h>
#include <string.h>
/* define simple structure */
struct {
unsigned int widthValidated;
unsigned int heightValidated;
} status1;
/* define a structure with bit fields */
struct {
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status2;
int main( ) {
printf( "Memory size occupied by status1 : %dn",
sizeof(status1));
printf( "Memory size occupied by status2 : %dn",
sizeof(status2));
return 0;
}
When the above code is compiled and executed, it produces the following result −
Memory size occupied by status1 : 8
Memory size occupied by status2 : 4
Bit Field Declaration
The declaration of a bit-field has the following form inside a structure −
struct {
type [member_name] : width ;
};
The following table describes the variable elements of a bit field −
Sr.No. Element & Description
1
type
An integer type that determines how a bit-field's value is interpreted. The type may be int, signed
int, or unsigned int.
2
member_name
The name of the bit-field.
3
width
The number of bits in the bit-field. The width must be less than or equal to the bit width of the
specified type.
The variables defined with a predefined width are called bit fields. A bit field can hold
more than a single bit; for example, if you need a variable to store a value from 0 to
7, then you can define a bit field with a width of 3 bits as follows −
struct {
unsigned int age : 3;
} Age;
The above structure definition instructs the C compiler that the age variable is going
to use only 3 bits to store the value. If you try to use more than 3 bits, then it will not
allow you to do so. Let us try the following example −
#include <stdio.h>
#include <string.h>
struct {
unsigned int age : 3;
} Age;
int main( ) {
Age.age = 4;
printf( "Sizeof( Age ) : %dn", sizeof(Age) );
printf( "Age.age : %dn", Age.age );
Age.age = 7;
printf( "Age.age : %dn", Age.age );
Age.age = 8;
printf( "Age.age : %dn", Age.age );
return 0;
}
When the above code is compiled it will compile with a warning and when executed,
it produces the following result −
Sizeof( Age ) : 4
Age.age : 4
Age.age : 7
Age.age : 0
Summary of Struct bit fields
 On occasion it is desired to hold a number of small integer items in a structure.
 To save space, the fields within a structure are not required to occupy a full
word. Instead, they can occupy a specified number of bits.
 Multiple consecutive bit fields in a structure will share a single word of
memory, insofar as each field fits completely. This reduces storage
requirements, at the expense of slower execution.
 If the next bit field does not fit in the currently unallocated portion of the
current word, then it will be put entirely into the next word. The remainder of
the current word will be wasted.
 The size of a bit field is indicated by appending a colon and the number of
desired bits after the field name.
 If a bit field size is specified as zero, it forces the next bit field to be placed on a
word boundary. These variables are more quickly accessed. The field name is
not required for zero length bit fields.
 Structure bit fields must be of an integral type. Most implementations treat
them as unsigned.
 Example: struct Packed_data {
unsigned int is_element:1; /* = 1 if element *
unsigned int atomic_number:8; /* Maximum 128 */
unsigned int is_reactant:1;
unsigned int is_product:1;
unsigned int is_catalyst:1;
unsigned int Stock_Index:16; /* Maximum 65,535 */
} chemical_inventory[ 10000 ];
 Each data item in the above array takes up one 32-bit word ( with four bits
wasted ), for a total of 10,000 words of storage for the entire array, as
opposed to 60,000 words of storage if bitfields were not used.
Enumerated Data Types
 Enumerated data types are a special form of integers, with the following
constraints:
o Only certain pre-determined values are allowed.
o Each valid value is assigned a name, which is then normally used
instead of integer values when working with this data type.
 Enumerated types, variables, and typedefs, operate similarly to structs:
enum suits { CLUBS, HEARTS, SPADES, DIAMONDS, NOTRUMP } trump;
enum suits ew_bid, ns_bid;
typedef enum Direction{ NORTH, SOUTH, EAST, WEST } Direction;
Direction nextMove = NORTH;
 Values may be assigned to specific enum value names.
o Any names without assigned values will get one higher than the
previous entry.
o If the first name does not have an assigned value, it gets the value of
zero.
o It is even legal to assign the same value to more than one name.
o Example:
 enum Errors{ NONE=0, // Redundant. The first one would be
zero anyway
 MINOR1=100, MINOR2, MINOR3, // 100, 101, and 102
MAJOR1=1000, MAJOR2, DIVIDE_BY_ZERO=1000 }; // 1000,
1001, and 1000 again.
 Because enumerated data types are integers, they can be used anywhere
integers are allowed. One of the best places in in switch statements:
switch( nextMove ) {
case NORTH:
y++;
break;
// etc.
 The compiler will allow the use of ordinary integers with enumerated
variables, e.g. trump = 2; , but it is bad practice.
Union
 Unions are declared, created, and used exactly the same as
struts, EXCEPT for one key difference:
o Structs allocate enough space to store all of the fields in the struct.
The first one is stored at the beginning of the struct, the second is
stored after that, and so on.
o Unions only allocate enough space to store the largest field listed, and
all fields are stored at the same space - The beginnion of the union.
 This means that all fields in a union share the same space, which can be used
for any listed field but not more than one of them.
 In order to know which union field is actually stored, unions are often nested
inside of structs, with an enumerated type indicating what is actually stored
there. For example:
struct Flight {
enum { PASSENGER, CARGO } type;
union {
int npassengers;
double tonnages; // Units are not necessarily tons.
} cargo;
};
Struct Flight flights[ 1000 ];
flights[ 42 ].type = PASSENGER;
flights[ 42 ].cargo.npassengers = 150;
flights[ 20 ].type = CARGO;
flights[ 20 ].cargo.tonnages = 356.78;
 The example above does not actually save any space, because the 4 bytes
saved by using a union instead of a struct for the cargo is lost by the int
needed for the enumerated type. However a lot of space could potentially be
saved if the items in the union were larger, such as nested structs or large
arrays.
 Unions are sometimes also used to break up larger data items into smaller
pieces, such as this code to extract four 8-bit bytes from a 32-bit int:
int nRead;
union {
unsigned int n;
unsigned char c[ 4 ];
} data;
// ( Code to read in nRead, from the user or a file, has been
omitted in this example )
data.n = nRead;
for( int i = 0; i < 4; i++ )
printf( "Byte number %d of %ud is %udn", i, nRead, data.c[ i ]
);

More Related Content

What's hot (20)

C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Python strings
Python stringsPython strings
Python strings
 
Strings in c
Strings in cStrings in c
Strings in c
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Strings
StringsStrings
Strings
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Branching in C
Branching in CBranching in C
Branching in C
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Finite automata
Finite automataFinite automata
Finite automata
 
Chap 11(pointers)
Chap 11(pointers)Chap 11(pointers)
Chap 11(pointers)
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiArrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
 

Similar to C - Optimize Memory with Bit Fields and Enumerated Data Types

structenumtypedefunion.pptx
structenumtypedefunion.pptxstructenumtypedefunion.pptx
structenumtypedefunion.pptxKUPPALAPADMINI
 
C PROGRAMMING LANGUAGE
C  PROGRAMMING  LANGUAGEC  PROGRAMMING  LANGUAGE
C PROGRAMMING LANGUAGEPRASANYA K
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Typesk v
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data typesManisha Keim
 
COMPLEX AND USER DEFINED TYPESPlease note that the material on t.docx
COMPLEX AND USER DEFINED TYPESPlease note that the material on t.docxCOMPLEX AND USER DEFINED TYPESPlease note that the material on t.docx
COMPLEX AND USER DEFINED TYPESPlease note that the material on t.docxdonnajames55
 
Lesson 23. Pattern 15. Growth of structures' sizes
Lesson 23. Pattern 15. Growth of structures' sizesLesson 23. Pattern 15. Growth of structures' sizes
Lesson 23. Pattern 15. Growth of structures' sizesPVS-Studio
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfYRABHI
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programmingHarshita Yadav
 
structures and unions in 'C'
structures and unions in 'C'structures and unions in 'C'
structures and unions in 'C'illpa
 
CIS 1403 Lab 2- Data Types and Variables
CIS 1403 Lab 2- Data Types and VariablesCIS 1403 Lab 2- Data Types and Variables
CIS 1403 Lab 2- Data Types and VariablesHamad Odhabi
 
Simple Queriebhjjnhhbbbbnnnnjjs In SQL.pdf
Simple Queriebhjjnhhbbbbnnnnjjs In SQL.pdfSimple Queriebhjjnhhbbbbnnnnjjs In SQL.pdf
Simple Queriebhjjnhhbbbbnnnnjjs In SQL.pdfManojVishwakarma91
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programmingnmahi96
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Dushmanta Nath
 

Similar to C - Optimize Memory with Bit Fields and Enumerated Data Types (20)

structenumtypedefunion.pptx
structenumtypedefunion.pptxstructenumtypedefunion.pptx
structenumtypedefunion.pptx
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
C PROGRAMMING LANGUAGE
C  PROGRAMMING  LANGUAGEC  PROGRAMMING  LANGUAGE
C PROGRAMMING LANGUAGE
 
C++ data types
C++ data typesC++ data types
C++ data types
 
Data types ,variables,array
Data types ,variables,arrayData types ,variables,array
Data types ,variables,array
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
 
COMPLEX AND USER DEFINED TYPESPlease note that the material on t.docx
COMPLEX AND USER DEFINED TYPESPlease note that the material on t.docxCOMPLEX AND USER DEFINED TYPESPlease note that the material on t.docx
COMPLEX AND USER DEFINED TYPESPlease note that the material on t.docx
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Lesson 23. Pattern 15. Growth of structures' sizes
Lesson 23. Pattern 15. Growth of structures' sizesLesson 23. Pattern 15. Growth of structures' sizes
Lesson 23. Pattern 15. Growth of structures' sizes
 
Arrays
ArraysArrays
Arrays
 
Data types in C
Data types in CData types in C
Data types in C
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdf
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
structures and unions in 'C'
structures and unions in 'C'structures and unions in 'C'
structures and unions in 'C'
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
 
CIS 1403 Lab 2- Data Types and Variables
CIS 1403 Lab 2- Data Types and VariablesCIS 1403 Lab 2- Data Types and Variables
CIS 1403 Lab 2- Data Types and Variables
 
Simple Queriebhjjnhhbbbbnnnnjjs In SQL.pdf
Simple Queriebhjjnhhbbbbnnnnjjs In SQL.pdfSimple Queriebhjjnhhbbbbnnnnjjs In SQL.pdf
Simple Queriebhjjnhhbbbbnnnnjjs In SQL.pdf
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programming
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

C - Optimize Memory with Bit Fields and Enumerated Data Types

  • 2. C - Bit Fields Suppose your C program contains a number of TRUE/FALSE variables grouped in a structure called status, as follows − struct { unsigned int widthValidated; unsigned int heightValidated; } status; This structure requires 8 bytes of memory space but in actual, we are going to store either 0 or 1 in each of the variables. The C programming language offers a better way to utilize the memory space in such situations. If you are using such variables inside a structure then you can define the width of a variable which tells the C compiler that you are going to use only those number of bytes. For example, the above structure can be re-written as follows − struct { unsigned int widthValidated : 1; unsigned int heightValidated : 1; } status; The above structure requires 4 bytes of memory space for status variable, but only 2 bits will be used to store the values. If you will use up to 32 variables each one with a width of 1 bit, then also the status structure will use 4 bytes. However as soon as you have 33 variables, it will allocate the next slot of the memory and it will start using 8 bytes. Let us check the following example to understand the concept − #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned int widthValidated; unsigned int heightValidated; } status1; /* define a structure with bit fields */ struct { unsigned int widthValidated : 1; unsigned int heightValidated : 1; } status2; int main( ) { printf( "Memory size occupied by status1 : %dn", sizeof(status1)); printf( "Memory size occupied by status2 : %dn", sizeof(status2)); return 0; }
  • 3. When the above code is compiled and executed, it produces the following result − Memory size occupied by status1 : 8 Memory size occupied by status2 : 4 Bit Field Declaration The declaration of a bit-field has the following form inside a structure − struct { type [member_name] : width ; }; The following table describes the variable elements of a bit field − Sr.No. Element & Description 1 type An integer type that determines how a bit-field's value is interpreted. The type may be int, signed int, or unsigned int. 2 member_name The name of the bit-field. 3 width The number of bits in the bit-field. The width must be less than or equal to the bit width of the specified type. The variables defined with a predefined width are called bit fields. A bit field can hold more than a single bit; for example, if you need a variable to store a value from 0 to 7, then you can define a bit field with a width of 3 bits as follows − struct { unsigned int age : 3; } Age; The above structure definition instructs the C compiler that the age variable is going to use only 3 bits to store the value. If you try to use more than 3 bits, then it will not allow you to do so. Let us try the following example − #include <stdio.h> #include <string.h> struct { unsigned int age : 3; } Age;
  • 4. int main( ) { Age.age = 4; printf( "Sizeof( Age ) : %dn", sizeof(Age) ); printf( "Age.age : %dn", Age.age ); Age.age = 7; printf( "Age.age : %dn", Age.age ); Age.age = 8; printf( "Age.age : %dn", Age.age ); return 0; } When the above code is compiled it will compile with a warning and when executed, it produces the following result − Sizeof( Age ) : 4 Age.age : 4 Age.age : 7 Age.age : 0 Summary of Struct bit fields  On occasion it is desired to hold a number of small integer items in a structure.  To save space, the fields within a structure are not required to occupy a full word. Instead, they can occupy a specified number of bits.  Multiple consecutive bit fields in a structure will share a single word of memory, insofar as each field fits completely. This reduces storage requirements, at the expense of slower execution.  If the next bit field does not fit in the currently unallocated portion of the current word, then it will be put entirely into the next word. The remainder of the current word will be wasted.  The size of a bit field is indicated by appending a colon and the number of desired bits after the field name.  If a bit field size is specified as zero, it forces the next bit field to be placed on a word boundary. These variables are more quickly accessed. The field name is not required for zero length bit fields.  Structure bit fields must be of an integral type. Most implementations treat them as unsigned.  Example: struct Packed_data { unsigned int is_element:1; /* = 1 if element * unsigned int atomic_number:8; /* Maximum 128 */ unsigned int is_reactant:1; unsigned int is_product:1; unsigned int is_catalyst:1; unsigned int Stock_Index:16; /* Maximum 65,535 */ } chemical_inventory[ 10000 ];
  • 5.  Each data item in the above array takes up one 32-bit word ( with four bits wasted ), for a total of 10,000 words of storage for the entire array, as opposed to 60,000 words of storage if bitfields were not used.
  • 6. Enumerated Data Types  Enumerated data types are a special form of integers, with the following constraints: o Only certain pre-determined values are allowed. o Each valid value is assigned a name, which is then normally used instead of integer values when working with this data type.  Enumerated types, variables, and typedefs, operate similarly to structs: enum suits { CLUBS, HEARTS, SPADES, DIAMONDS, NOTRUMP } trump; enum suits ew_bid, ns_bid; typedef enum Direction{ NORTH, SOUTH, EAST, WEST } Direction; Direction nextMove = NORTH;  Values may be assigned to specific enum value names. o Any names without assigned values will get one higher than the previous entry. o If the first name does not have an assigned value, it gets the value of zero. o It is even legal to assign the same value to more than one name. o Example:  enum Errors{ NONE=0, // Redundant. The first one would be zero anyway  MINOR1=100, MINOR2, MINOR3, // 100, 101, and 102 MAJOR1=1000, MAJOR2, DIVIDE_BY_ZERO=1000 }; // 1000, 1001, and 1000 again.  Because enumerated data types are integers, they can be used anywhere integers are allowed. One of the best places in in switch statements: switch( nextMove ) { case NORTH: y++; break; // etc.  The compiler will allow the use of ordinary integers with enumerated variables, e.g. trump = 2; , but it is bad practice.
  • 7. Union  Unions are declared, created, and used exactly the same as struts, EXCEPT for one key difference: o Structs allocate enough space to store all of the fields in the struct. The first one is stored at the beginning of the struct, the second is stored after that, and so on. o Unions only allocate enough space to store the largest field listed, and all fields are stored at the same space - The beginnion of the union.  This means that all fields in a union share the same space, which can be used for any listed field but not more than one of them.  In order to know which union field is actually stored, unions are often nested inside of structs, with an enumerated type indicating what is actually stored there. For example: struct Flight { enum { PASSENGER, CARGO } type; union { int npassengers; double tonnages; // Units are not necessarily tons. } cargo; }; Struct Flight flights[ 1000 ]; flights[ 42 ].type = PASSENGER; flights[ 42 ].cargo.npassengers = 150; flights[ 20 ].type = CARGO; flights[ 20 ].cargo.tonnages = 356.78;  The example above does not actually save any space, because the 4 bytes saved by using a union instead of a struct for the cargo is lost by the int needed for the enumerated type. However a lot of space could potentially be saved if the items in the union were larger, such as nested structs or large arrays.  Unions are sometimes also used to break up larger data items into smaller pieces, such as this code to extract four 8-bit bytes from a 32-bit int: int nRead; union { unsigned int n; unsigned char c[ 4 ]; } data; // ( Code to read in nRead, from the user or a file, has been omitted in this example ) data.n = nRead; for( int i = 0; i < 4; i++ ) printf( "Byte number %d of %ud is %udn", i, nRead, data.c[ i ] );