-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC4_NULL_malloc_calloc_realloc_free.c
125 lines (98 loc) · 4.42 KB
/
C4_NULL_malloc_calloc_realloc_free.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* @Author: Daniel Maurelle
* @Email: daniel.maurelle@gmail.com
* Youtube: https://youtube.com/playlist?list=PLDqCEJxpkYKCzZSwAz-2PpzcwMDY11NGp&si=HRSvwoUxsEBtFsfI
* @Date: 12-18-2023 18:20:55
* @Description: 4-NULL, malloc, calloc, realloc, free.
*
* MIT License
*
* Copyright 2023 Daniel Maurelle
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Pointer initialization to NULL
int *ptr = NULL;
// Allocating memory using malloc
ptr = (int*) malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Initialize the allocated memory with zeros
memset(ptr, 0, 5 * sizeof(int));
// Printing the values to show they are initialized to zero
printf("Values after malloc and memset:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
// Reallocating memory using realloc
ptr = (int*) realloc(ptr, 10 * sizeof(int));
if (ptr == NULL) {
printf("Memory reallocation failed\n");
free(ptr);
ptr = NULL; // Set pointer to NULL after freeing
return 1;
}
// Allocating memory using calloc
int *cptr = (int*) calloc(5, sizeof(int));
if (cptr == NULL) {
printf("Memory allocation with calloc failed\n");
free(cptr);
cptr = NULL; // Set pointer to NULL after freeing
return 1;
}
// Printing the values to show they are initialized to zero
printf("Values after calloc:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", cptr[i]);
}
printf("\n");
// Freeing the memory and setting pointers to NULL
free(ptr);
ptr = NULL; // Set pointer to NULL after freeing
free(cptr);
cptr = NULL; // Set pointer to NULL after freeing
return 0;
}
/*
In this program:
Pointer Initialization: int *ptr = NULL; initializes a pointer to NULL, ensuring it doesn't point to a random memory location.
Memory Allocation with malloc: malloc allocates uninitialized memory. The allocated memory is then manually initialized to zero
using memset.
Memory Re-allocation with realloc: realloc is used to resize the allocated memory. It's important to handle the case where realloc
may fail and return NULL.
Memory Allocation with calloc: calloc allocates memory and automatically initializes it to zero. This is shown with the allocation
and printing of the cptr array.
Freeing Memory: Both ptr and cptr are freed at the end of the program to prevent memory leaks.
After each free call, the pointer is immediately set to NULL. This prevents the pointer from becoming a dangling pointer.
It's important to note that setting a pointer to NULL after freeing doesn't affect the freed memory itself; it only ensures
that the pointer doesn't point to an invalid memory location anymore.
This practice enhances the safety and maintainability of the code, especially in large and complex applications.
Remember, in a real embedded system, you should always check the return values of malloc, calloc, and realloc to ensure
that memory allocation was successful.
Additionally, memory allocation functions behave slightly differently on different systems, especially in constrained environments
like embedded systems.
It's crucial to understand your system's characteristics and limitations when working with dynamic memory allocation.
*/