-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocate.c
35 lines (29 loc) · 1.13 KB
/
allocate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>
int main() {
// Allocating memory dynamically on the heap
// for an integer, which returns an int* pointer
int* a = (int*) malloc (sizeof(int));
if (a != NULL)
printf("Allocated memory for an integer\n");
else
printf("malloc() failed");
int x = 100;
// We can update the value to this location, since
// we have allocated memory for the pointer
*a = x;
printf("a points to %d\n", *a);
int* b = (int*) malloc (sizeof(int));
if (b != NULL)
printf("Allocated memory for an integer\n");
else
printf("malloc() failed");
x = 100;
// Will give a segmentation fault, since
// *b is not allocated any memory
*b = x;
printf("b points to %d\n", *b);
// Free memory from the heap
free(a);
return 0;
} void* malloc(size_t size);