 Unit - II Dynamic memory allocation, Pointers and
Functions, Pointers and structures: 9
Dynamic memory allocation, Function pointers –
Dynamic memory allocation, Function pointers –
calling a function using a function pointer–
Structures – Introduction – Structures in Functions
–Pointers to structures-Accessing structure
members - Using pointer as a function argument -
Array of structures – self referential structures.
int a[9];
 The process of allocating memory at runtime
Static Memory Allocation Dynamic Memory Allocation
Memory is allocated at compile
time.
Memory is allocated at run time.
time.
Memory is allocated at run time.
Memory can't be increased
while executing program.
Memory can be increased while
executing program.
Used in array. Used in linked list.
1. malloc()
2. calloc() <stdlib.h>
3. free()
4. realloc()
 malloc() function stands for memory allocation
 Allocates single block of memory with requested
size and returns the pointer (of type void) to the
size and returns the pointer (of type void) to the
first byte of allocated space.
 Returns NULL if memory is not sufficient.
 Syntax
ptr = malloc (byte_size);
Example(cast_type *)
 Example(cast_type *)
int *ptr;
ptr = (int*)malloc(5 * sizeof(int));
 Example
int *ptr;
ptr = (int*)malloc(5 * sizeof(int));
ptr = (int*)malloc(5 * sizeof(int));
← 20 Bytes of memory →
ptr
#include <stdio.h> #include <stdlib.h>
int main(){
int* ptr; int n, i;
n = 5;
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.n");
exit(0);
}
}
else {
for (i = 0; i < n; ++i)
ptr[i] = i + 1;
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
}
return 0;}
 calloc() function stands for contiguous allocation
 Allocates the space for elements of an array.
Initializes the elements to zero and returns a
Initializes the elements to zero and returns a
pointer to the memory.
 Returns NULL if memory is not sufficient.
 Syntax
ptr = (cast_type *) calloc (number,byte-size);
 Example
 Example
ptr = (int *) calloc(5, sizeof(int));
 Example
int *ptr;
ptr = (int*)calloc(5 , sizeof(int));
ptr = (int*)calloc(5 , sizeof(int));
← 20 Bytes of memory →
ptr
#include <stdio.h> #include <stdlib.h>
int main(){
int* ptr; int n, i;
n = 5;
ptr = (int*)calloc(n , sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.n");
exit(0);
}
}
else {
for (i = 0; i < n; ++i)
ptr[i] = i + 1;
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
}
return 0;}
 malloc() function allocates memory and leaves
the memory uninitialized
 calloc() function allocates memory and
 calloc() function allocates memory and
initializes all bits to zero
 Frees or empties the previously allocated
memory space.
 Syntax
 Syntax
free(ptr);
#include <stdio.h> #include <stdlib.h>
int main(){
int* ptr; int n, i;
n = 5;
ptr = (int*)calloc(n , sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.n");
exit(0);
}
}
else {
for (i = 0; i < n; ++i)
ptr[i] = i + 1;
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
}
free(ptr);
return 0;}
 Modify the size of previously allocated memory
space
If memory is not sufficient for malloc() or calloc(), the
If memory is not sufficient for malloc() or calloc(), the
memory can eb reallocated by realloc() function
 Syntax
ptr=realloc(ptr, new-size)
Example
 Example
int *ptr;
ptr = (int*)malloc(50 * sizeof(int));
ptr = (int *) realloc(ptr, 60*sizeof(int));
 Syntax
ptr=realloc(ptr, new-size)
Example
 Example
int *ptr;
ptr = (int*)malloc(5 * sizeof(int));
ptr = (int *) realloc(ptr, 6*sizeof(int));
int *ptr;
ptr = (int*)malloc(5 * sizeof(int));
ptr
← 20 Bytes of memory →
ptr = (int *) realloc(ptr,6*sizeof(int));
← 24 Bytes of memory →
ptr
#include <stdio.h>
int main () {
char *ptr;
ptr = (char *) malloc(10);
strcpy(ptr, "Programming");
printf(" %s, Address = %un", ptr, ptr);
printf(" %s, Address = %un", ptr, ptr);
ptr = (char *) realloc(ptr, 20); //ptr is reallocated with new size
strcat(ptr, " In 'C'");
printf(" %s, Address = %un", ptr, ptr);
free(ptr);
return 0;
}
Dynamic memory allocation

Dynamic memory allocation

  • 2.
     Unit -II Dynamic memory allocation, Pointers and Functions, Pointers and structures: 9 Dynamic memory allocation, Function pointers – Dynamic memory allocation, Function pointers – calling a function using a function pointer– Structures – Introduction – Structures in Functions –Pointers to structures-Accessing structure members - Using pointer as a function argument - Array of structures – self referential structures.
  • 3.
  • 4.
     The processof allocating memory at runtime
  • 5.
    Static Memory AllocationDynamic Memory Allocation Memory is allocated at compile time. Memory is allocated at run time. time. Memory is allocated at run time. Memory can't be increased while executing program. Memory can be increased while executing program. Used in array. Used in linked list.
  • 6.
    1. malloc() 2. calloc()<stdlib.h> 3. free() 4. realloc()
  • 7.
     malloc() functionstands for memory allocation  Allocates single block of memory with requested size and returns the pointer (of type void) to the size and returns the pointer (of type void) to the first byte of allocated space.  Returns NULL if memory is not sufficient.
  • 8.
     Syntax ptr =malloc (byte_size); Example(cast_type *)  Example(cast_type *) int *ptr; ptr = (int*)malloc(5 * sizeof(int));
  • 9.
     Example int *ptr; ptr= (int*)malloc(5 * sizeof(int)); ptr = (int*)malloc(5 * sizeof(int)); ← 20 Bytes of memory → ptr
  • 10.
    #include <stdio.h> #include<stdlib.h> int main(){ int* ptr; int n, i; n = 5; ptr = (int*)malloc(n * sizeof(int)); if (ptr == NULL) { printf("Memory not allocated.n"); exit(0); } } else { for (i = 0; i < n; ++i) ptr[i] = i + 1; printf("The elements of the array are: "); for (i = 0; i < n; ++i) printf("%d, ", ptr[i]); } return 0;}
  • 11.
     calloc() functionstands for contiguous allocation  Allocates the space for elements of an array. Initializes the elements to zero and returns a Initializes the elements to zero and returns a pointer to the memory.  Returns NULL if memory is not sufficient.
  • 12.
     Syntax ptr =(cast_type *) calloc (number,byte-size);  Example  Example ptr = (int *) calloc(5, sizeof(int));
  • 13.
     Example int *ptr; ptr= (int*)calloc(5 , sizeof(int)); ptr = (int*)calloc(5 , sizeof(int)); ← 20 Bytes of memory → ptr
  • 14.
    #include <stdio.h> #include<stdlib.h> int main(){ int* ptr; int n, i; n = 5; ptr = (int*)calloc(n , sizeof(int)); if (ptr == NULL) { printf("Memory not allocated.n"); exit(0); } } else { for (i = 0; i < n; ++i) ptr[i] = i + 1; printf("The elements of the array are: "); for (i = 0; i < n; ++i) printf("%d, ", ptr[i]); } return 0;}
  • 15.
     malloc() functionallocates memory and leaves the memory uninitialized  calloc() function allocates memory and  calloc() function allocates memory and initializes all bits to zero
  • 16.
     Frees orempties the previously allocated memory space.  Syntax  Syntax free(ptr);
  • 17.
    #include <stdio.h> #include<stdlib.h> int main(){ int* ptr; int n, i; n = 5; ptr = (int*)calloc(n , sizeof(int)); if (ptr == NULL) { printf("Memory not allocated.n"); exit(0); } } else { for (i = 0; i < n; ++i) ptr[i] = i + 1; printf("The elements of the array are: "); for (i = 0; i < n; ++i) printf("%d, ", ptr[i]); } free(ptr); return 0;}
  • 18.
     Modify thesize of previously allocated memory space If memory is not sufficient for malloc() or calloc(), the If memory is not sufficient for malloc() or calloc(), the memory can eb reallocated by realloc() function
  • 19.
     Syntax ptr=realloc(ptr, new-size) Example Example int *ptr; ptr = (int*)malloc(50 * sizeof(int)); ptr = (int *) realloc(ptr, 60*sizeof(int));
  • 20.
     Syntax ptr=realloc(ptr, new-size) Example Example int *ptr; ptr = (int*)malloc(5 * sizeof(int)); ptr = (int *) realloc(ptr, 6*sizeof(int));
  • 21.
    int *ptr; ptr =(int*)malloc(5 * sizeof(int)); ptr ← 20 Bytes of memory → ptr = (int *) realloc(ptr,6*sizeof(int)); ← 24 Bytes of memory → ptr
  • 22.
    #include <stdio.h> int main() { char *ptr; ptr = (char *) malloc(10); strcpy(ptr, "Programming"); printf(" %s, Address = %un", ptr, ptr); printf(" %s, Address = %un", ptr, ptr); ptr = (char *) realloc(ptr, 20); //ptr is reallocated with new size strcat(ptr, " In 'C'"); printf(" %s, Address = %un", ptr, ptr); free(ptr); return 0; }