• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <new>
4 
main()5 int main()
6 {
7     int n = 10;
8 
9     // Dynamically allocate memory using malloc()
10     int *malloc_ptr = (int *)malloc(n * sizeof(int));
11 
12     if (malloc_ptr != NULL)
13     {
14         //Just Feeding data
15         for (int i = 0; i < n; ++i)
16         {
17             malloc_ptr[i] = i + 1;
18         }
19         //For checking realloc
20         int new_n = 20;
21         malloc_ptr = (int *)realloc(malloc_ptr, new_n * sizeof(int));
22         for (int i = 0; i < new_n; ++i)
23         {
24             malloc_ptr[i] = i + 1;
25         }
26         //For checking free
27         free(malloc_ptr);
28     }
29 
30     // Dynamically allocating memory using operator new[]
31     int *new_ptr = new int[n];
32     if (new_ptr != NULL)
33     {
34         //Just feeding data
35         for (int i = 0; i < n; ++i)
36         {
37             new_ptr[i] = i + 1;
38         }
39 
40         //For checking operator delete[]
41         delete[] new_ptr;
42     }
43     printf("Done");
44     return 0;
45 }