-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc.c
39 lines (24 loc) · 884 Bytes
/
malloc.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
35
36
37
38
39
// test of malloc()
#include <stdio.h>
#include <stdlib.h> // requiered for the malloc() use
int main(void)
{
int *allocatedMemory = NULL; //pointer on the int
allocatedMemory = malloc(sizeof(int)); // the malloc funnction gives an adress to the pointer
/*-----------------------memory allocation test-------------------------*/
/*
if(allocatedMemory == NULL) //if allocation failed, we stop the program with exit()
{
exit(0);
}else {
printf("%p\n", allocatedMemory);
}
*/
/*----------------------------------------------------------------------*/
printf("how old are u ?\n> ");
scanf("%d", allocatedMemory);
printf("you are %d years old\n", *allocatedMemory);
free(allocatedMemory);
return 0;
}
//here the memory is being freed manually, but it's the same automaticlly at the end of the function