Вы находитесь на странице: 1из 21

Bit fields in C

when the member variables of a structure represent


some flags that store either 0 or 1.
struct info
{
int isMemoryFreed;
int isObjectAllocated;
}
•Though a value of 0 or 1 would be stored in these variables but the
memory used would be complete 8 bytes.

•To reduce memory consumption when it is known that only some bits
would be used for a variable, the concept of bit fields can be used.
Bit fields definition:
struct info
{
int isMemoryFreed : 1;
int isObjectAllocated : 1;
}
• The above declaration tells the compiler that only 1 bit
each from the two variables would be used.

• After seeing this, the compiler reduces the memory size of


the structure.
#include <stdio.h>

struct example1
{
int isMemoryAllocated;
int isObjectAllocated;
};

struct example2
{
int isMemoryAllocated : 1;
int isObjectAllocated : 1;
};
int main(void)
{
printf("\n sizeof example1 is [%u], sizeof
example2 is [%u]\n", sizeof(struct example1),
sizeof(struct example2));

return 0;
}
Dynamic memory allocation in C:
• The process of allocating memory during
program execution is called dynamic memory
allocation.
•Dynamic memory allocation functions in C:
•C language offers 4 dynamic memory allocation
functions. They are,
malloc()
calloc()
realloc()
free()
Function Description
allocates requested size of
malloc() bytes and returns a void
pointer pointing to the first
byte of the allocated space
allocates space for an array
calloc() of elements, initialize them
to zero and then returns a
void pointer to the memory
releases previously allocated
free memory
modify the size of previously
realloc allocated space
malloc ( )
Syntax
malloc (number *sizeof(int));
malloc() function in C:
•malloc () function is used to allocate space in
memory during the execution of the program.
• It does not initialize the memory allocated during
execution. It carries garbage value.
• It returns null pointer if it couldn’t able to allocate
requested amount of memory.
malloc() function in C:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Two array is allocated\n");
n=2;
int *A = (int*)malloc(n*sizeof(int));
printf("%d \n",A[0]);
printf("%d",A[1]);
free(A); A=NULL; return 0;
}
calloc ( )
calloc() function in C:
•calloc () function is also like malloc () function. But
calloc () initializes the allocated memory to zero.
But, malloc() doesn’t.
calloc() function in C:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Two array is allocated\n");
n=2;
int *A = (int*)calloc(n,sizeof(int));
printf("%d \n",A[0]);
printf("%d",A[1]);
free(A); A=NULL; return 0;
}
Difference
between malloc() and calloc()
calloc() malloc()
calloc() initializes the malloc() initializes the
allocated memory with 0 allocated memory with
value. garbage values.

Number of arguments is 2 Number of argument is 1

Syntax : Syntax :
(cast_type *)calloc(blocks , (cast_type
size_of_block); *)malloc(Size_in_bytes);
realloc ( )
realloc() function in C:
•realloc () function modifies the allocated memory
size by malloc () and calloc () functions to new size.
•If enough space doesn’t exist in memory of current
block to extend, new block is allocated for the full
size of reallocation, then copies the existing data to
new block and then frees the old block.

realloc() changes memory size that is already


allocated dynamically to a variable.
realloc() function in C:
Syntax:

void* realloc(pointer, new-size)

Time for an Example: realloc()

int *x;
x = (int*)malloc(50 * sizeof(int));
x = (int*)realloc(x,100); /*allocated a new
memory to variable x/*
free() function in C:
•free () function frees the allocated memory by
malloc (), calloc (), realloc () functions and returns
the memory to the system.
/*C program to input and print text using
Dynamic Memory Allocation.*/ malloc
#include <stdio.h>
#include <stdlib.h> printf("Enter text: ");
int main() scanf(" "); /*clear input
{ buffer*/
int n; gets(text);
char *text; printf("Inputted text is:
printf("Enter limit of the text: %s\n",text);
"); /*Free Memory*/
scanf("%d",&n); free(text);
/*allocate memory
dynamically*/ return 0;
text=(char*)malloc(n*sizeof(char}
));
/*C program to input and print text using
Dynamic Memory Allocation.*/ calloc
#include <stdio.h>
#include <stdlib.h> printf("Enter text: ");
int main() scanf(" "); /*clear input
{ buffer*/
int n; gets(text);
char *text; printf("Inputted text is:
printf("Enter limit of the text: %s\n",text);
"); /*Free Memory*/
scanf("%d",&n); free(text);
/*allocate memory
dynamically*/ return 0;
text=(char*)calloc(n,sizeof(char) }
);
Static memory allocation Dynamic memory
allocation
• In static memory allocation, memory is allocated
while writing the C program. Actually, user
requested memory will be allocated at compile
time.
• Memory size can’t be modified while execution.

Example: array
• In dynamic memory allocation, memory is
allocated while executing the program. That
means at run time.
• Memory size can be modified while execution.

Example: Linked list


Linked lists are the best and simplest example of a
dynamic data structure that uses pointers for its
implementation.
Linked list
• Linked lists are the best example of a dynamic
data structure that uses pointers for its
implementation.

• Linked lists are a way to store data with structures


so that the programmer can automatically create a
new place to store data whenever necessary.
Linked list
•Linked lists have a few advantages over arrays:
•Items can be added or removed from the middle of
the list
•There is no need to define an initial size

Вам также может понравиться