You are on page 1of 24

DAY – 7

BASICS OF C
What will you learn?
 Dynamic Memory Allocation
 Structures
 Unions
 Structures versus Unions
MEMORY ALLOCATION
There are two types of memory allocations: static allocation and
dynamic allocation

1. Static allocation refers to allocation of memory for a variable at


compile time. They need declarations and definitions specified in
the source program. The number of bytes reserved cannot be
changed.

2. Dynamic allocation refers to allocation of memory for a variable


at run time. They use predefined functions to allocate and de-
allocate memory for data while the program is running. Memory
is allocated from heap through pointer.
MEMORY ALLOCATION
There are two types of memory allocations: static allocation and
dynamic allocation

1. Static allocation refers to allocation of memory for a variable at


compile time. They need declarations and definitions specified in
the source program. The number of bytes reserved cannot be
changed.

2. Dynamic allocation refers to allocation of memory for a variable


at run time. They use predefined functions to allocate and de-
allocate memory for data while the program is running. Memory
is allocated from heap through pointer.
DYNAMIC MEMORY MANAGEMENT
Consider an array
int marks [100];
DYNAMIC MEMORY ALLOCATION FUNCTIONS

The process of allocating memory at run time is known as


dynamic memory allocation. The C programming does not have this
function. But it offers it through memory management functions. The
memory management functions are used to allocate and de-allocate
storage during program execution. The functions are

1. malloc()

2. calloc()

3. free()

4. realloc()
MALLOC()
 This function is used to allocate one block of storage.

 It reserves the size specified by the pointer and returns void.

 The syntax for malloc( ) is as follows

void * malloc(size_t size);


X[0] X[1] X[2] X[3] 4 5 6 7 8 9
Example: - X 

char* x = (char*)malloc (10);

It is also used to allocate storage for complex data such as structures.


CALLOC()
 This function is used to allocate multiple blocks of memory.

 It is used to store data types such as arrays and structures.

 It allocates storage for multiple blocks and initializes them all to


zero and the pointer will point to the first byte of the first block.
Here all blocks are same size.

 The syntax is as follows:

ptr = (cast-type *) calloc (n, element-size);


 Example: -
x = (int *) calloc(200, sizeof(int)); // int x[200];
FREE()
 Whenever a variable is no longer then such variables memory is
released. It is done by using free function.

 The syntax is as follows:

void free(void *ptr);

 example:

int *p=(int *)malloc(400);

------

------

free (p);
REALLOC()
 Reallocation is done when

 Memory allocated to a variable is not sufficient then it is required to increase the


size
 Memory allocated to a variable is more than the required storage then it is
required to decrease the size
 The process that is carried in the above two statement is called reallocation.

 Syntax:

void* realloc(void *ptr, size_t newsize);


 Example: -

int *ptr = (int *)malloc (100);

---------

---------

ptr = realloc ( ptr,150);


STRUCTURES
“A structure is defined as a collection of dissimilar data types under one single name”.
STRUCTURE TYPE DECLARATION

struct tag_name struct book_bank


{ {
data type member 1; char title[25];
data type member 2; char author[20];
data type member 3; int pages;
... float price;
data type member n; };
};
DECLARING STRUCTURE VARIABLES
After defining the structure, we declare variables of the
structure data type. A structure variable declaration is similar to that of
an ordinary variable declaration.

Example: -

struct book_bank book1, book2, book3;

Here book1, book2, book3 are the structure variables of type


struct book_bank. Each of these variables can have the four members of
the structure book_bank.
INITIALIZING STRUCTURES
A structure can be initialized in the same way how the other variables
are initialized. Initializing a structure means assigning some values to the
members of the structure. The initial values are given enclosed in the braces and
are separated by commas, shown as follows:

Eg:

struct employee
struct employee e = { 45,”Ajay Kumar”, 25000};
{

int empno;

char name[20];

float salary;

};
MEMORY REPRESENTATION

E 145 Ajay Kumar 15000

empno name Salary


ACCESSING STRUCTURE ELEMENTS
Members/elements of the structure are accessed using an operator “.” which
is known as member access operator or direct selection operator.

struct book_bank
{
char title[25];
char author[20];
int pages;
float price;
} book1;

book1.price is the variable representing the price of book1 and can be


treated like any other ordinary variables.
UNIONS
Union is a user defined data type that allows memory to be shared by
multiple data items of different data types.
Memory allocated to the union is equal to the memory of the data
member that has the largest size, and it is shared by all the other members of
the union.
DECLARATION OF UNION
union union_name

{ union item

data-type member1; {

data-type member2; int m;

……………………….. float x;

data-type memberN; char c;

}; };
DECLARING UNION VARIABLES
Below is the syntax for declaring the union
union <union_name ><variable-name>;
Ex:
Union:-
union item
{
int m;
float x;
char c;
};
Union variable:- union item code;
MEMORY REPRESENTATION
union item
{
int m;
float x;
char c;
};
65497 65498 65499 65500

c
STRUCTURES
VERSUS
UNIONS
Difference Structure Union

Keyword The keyword struct is used The keyword union


to define a structure is used to define a
union
Memory Each member within the Memory allocated to
structure is assigned a the union is shared
separate memory location all the members of
the union

Size The size of the structure is The size of the union


equal to the sum of the is equal to the size
sizes of its members of the largest
member of the
union
Difference Structure Union
Value Altering the value of one Alter the value of
Altering member of structure does any of the member
not affect the members of will affect the other
the structure member values

Accessing Individual members can be Only one member


members accessed at a time can be accessed at
a time
Initialization All the members of a Only one member
of Members structure can be initialized of a union is
at a time initialized.

You might also like