Sunday, December 16, 2018

Structures, Union, and Memory Allocation

Structure Definition :

• Structure is a data type to store group of data with various of data type
• Structure component called member/field/element.
• Heterogeneous (various element data type)
• Structure in other programming language also called record
• Nested Structure is a structure with one of its element is another structure.
Structure Declaration :

Syntax
struct name_structure {
    dataType1 name_field1;
    dataType2 name_field2;
    …
};

Variable can be defined at declaration time
struct name_structure {
  dataType1 name_field1;
  dataType2 name_field2;
} name_variable_structure ;

Structure variable declaration
struct name_structure  name_variable_structure; 

Union Definition :
• Union is used for memory join. By using union, a memory location can be assigned for two or more variable with different data types
• Memory capacity used by union is the largest capacity used by any element of the union

Union Declaration :
Union Data Declaration
union name_union{
         typedata1   name_var1;
         typedata2   name_var2;
         ....
}   name_var_union;

Union Variable Declaration
union   name_union       name_var_union;

Memory Allocation Definition :
• Memory allocation:
  acquiring some memory space (RAM) managed by the OS to be used by a program.
• Memory de-allocation:
  releasing memory space (RAM) back to the OS.

No comments:

Post a Comment