Handwritten Text Recognition for manuscripts and early printed texts
Â
Introduction to c part -3
1. Recall
âą What are functions used for?
âą What happens when we declare a
varaible? Say int a;
âą What is the difference between array and
structure?
âą What is the output if i print *p, p, &p ?
3. When does memory
allocated in C?
âą There are two ways in which memory can
be allocated in C:
âby declaring variables
âby explicitly requesting space from C
âą Each case above space is allocated at
different sectors of RAM
4. Where does memory
allocated in C?
âby declaring variables -
Stack
âby explicitly requesting space from C
- Heap
6. Stack
âą Stack is the place where all the
variables that are declared and
initialized before runtime are stored.
âą Stack follows Last in First Out
order(LIFO)
âą When the program finishes stack will
release all the spaces allocated for
variables.
Stack
12
32
322
34
10. Heap
âą Heap is the area of memory used for
dynamic memory allocation
âą Programmer allocates memory manually
at heap ; and hence variable on the heap
must be destroyed manually. There is no
automatic release as in stack23 32
43
452
89
11. How to allocate space in heap?
âą C inbuilt functions
âMalloc() Allocates the
specified number of bytes
âCalloc() Allocates the
specified number of bytes
and initializes them to zero
âRealloc() Increases or decreases
the size of the specified
block of memory.
Reallocates it if needed
12. Malloc()
âą malloc() stands for "memory allocation"
âą The malloc() function dynamically
allocates memory when required.
âą This function allocates âsizeâ byte of
memory and returns a pointer to the first
byte or NULL if there is some kind of error
Syntax
malloc(size_in_bytes);
14. All are same !
int *p;
p=malloc(2); //allocates 2 bytes of
memory
p=malloc(sizeof(Int)); //sizeof() returns
size of any data type
so here it will be mallloc(2)
p=(int *)malloc(2); //malloc always return
void * pointer . So you
may type cast it into any data type.
but it is unnecessary as the
15. #include <stdio.h>
#include <stdlib.h>
void main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL)
{ printf("Error! memory not allocated."); exit(0); }
Printf(âEnter the elements of Arrayâ);
For(i=0;i<n;++i)
{
Scanf(â%dâ,ptr+i);
Sum+=*(ptr+i);
}
Printf(âsum=%dâ,sum);
free(ptr); // allocated memory is released
}
HEAPStack
n=10
1000ptr=1000
16. calloc()
âą Calloc() stands for "contiguous allocationâ
âą Allocates multiple blocks of memory each of
same size and sets all bytes to zero
âą Syntax
calloc(number_of_elements,size_in_byt
es);
Again we need a pointer to point to the
allocated space in heap
18. #include <stdio.h>
#include <stdlib.h>
void main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc
if(ptr==NULL)
{ printf("Error! memory not allocated."); exit(0); }
Printf(âEnter the elements of Arrayâ);
For(i=0;i<n;++i)
{
Scanf(â%dâ,ptr+i); // Scanf(â%dâ,&ptr[i]);
Sum+=*(ptr+i);
}
Printf(âsum=%dâ,sum);
free(ptr); // allocated memory is released
}
19. Malloc() Vs Calloc()
âą Allocates "size" bytes of memory.
âą The contents of
allocated memory are not changed.
i.e., the memory may contains
garbage values.
âą returns void pointer (void *). If
the allocation succeeds, a pointer
to the block of memory is returned.
âą Allocates
a region of memory large enough
to hold "n elements" of "size" bytes
each.
âą The allocated region is initialized
to zero.
âą returns void pointer (void *). If
the allocation succeeds, a pointer
to the block of memory is returned.
21. Structure of Ram
TEXT
Also known as the code segment,
holds the executable instructions
of a program.
âą execute-only
âą fixed size
22. Structure of Ram
A global variable that is initialized
and stored in the data segment.
This section has read/write
attributes but cannot be shared
among processes running the
same program.
23. Structure of Ram
This section holds uninitialized
data. This data consists of global
variables that the system
initializes with 0s upon program
execution. Another name for this
section is the zero-initialized data
section.
24. Structure of Ram
When a program uses malloc() to
obtain dynamic memory, this
memory is placed in the heap.
25. Structure of Ram
This contains all the local
variables that get allocated.
When a function is called, the
local variables for that function
are pushed onto the stack.