-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC9-Checking_allocated_memory.c
165 lines (125 loc) · 4.71 KB
/
C9-Checking_allocated_memory.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* @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: 9-Checking allocated memory.
*
* 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.
*/
/*
In C, there is no standard function provided by the language or its libraries to directly check if a memory location pointed to
by a pointer is allocated and set (initialized). This is largely due to how memory management works in C. However, there are
some methods and best practices you can employ to manage this issue:
Initialization of Pointers: Always initialize your pointers. If a pointer is not pointing to a valid memory allocation,
it should be set to NULL. This way, you can check if a pointer is NULL before using it, which can prevent using uninitialized
or freed pointers.
*/
#include <stdio.h>
int main() {
int *ptr = NULL;
// Allocation
ptr = malloc(sizeof(int));
if (ptr != NULL) {
// Pointer is allocated
*ptr = 5; // Set value
}
}
/*
Checking for Successful Allocation: After calling memory allocation functions like malloc(), calloc(), or realloc(),
you should always check if the allocation was successful by verifying the pointer is not NULL.
*/
/*
int main() {
int *ptr = malloc(sizeof(int));
if (ptr == NULL) {
// Allocation failed
} else {
// Allocation was successful
}
}
*/
/*
Use of Flags or Structs: In some cases, you might use a structure that contains a flag or status field along with the pointer,
indicating whether the pointer has been allocated and set.
*/
/*
int main() {
typedef struct {
int *ptr;
int is_allocated;
} IntPointer;
IntPointer ip;
ip.ptr = malloc(sizeof(int));
ip.is_allocated = (ip.ptr != NULL);
}
*/
/*
Valgrind(Windows/Mac) and Other Memory Debugging Tools like Leaks(on Mac): Tools like Valgrind/Leaks can be used to detect memory leaks, use of uninitialized memory,
and other memory-related issues. They are very useful during the development phase to ensure proper memory management.
Custom Memory Management Functions: In some scenarios, you might implement custom allocation and deallocation functions
that internally track allocations, initializations, and deallocations.
*/
/*
// Custom memory allocation function
void* custom_malloc(size_t size) {
void *ptr = malloc(size);
if (ptr) {
printf("Allocated %zu bytes at %p\n", size, ptr);
} else {
printf("Memory allocation failed\n");
}
return ptr;
}
// Custom memory initialization function
void* custom_memset(void *ptr, int value, size_t num) {
if (ptr) {
memset(ptr, value, num);
printf("Initialized memory at %p with %d for %zu bytes\n", ptr, value, num);
}
return ptr;
}
// Custom memory deallocation function
void custom_free(void *ptr) {
if (ptr) {
free(ptr);
printf("Freed memory at %p\n", ptr);
}
}
int main() {
// Example usage
int *numbers = (int*) custom_malloc(10 * sizeof(int));
if (numbers) {
custom_memset(numbers, 0, 10 * sizeof(int));
// Use the allocated memory...
custom_free(numbers);
}
return 0;
}
*/
/*
Remember, in C, direct memory management is the programmer's responsibility, and it requires careful handling to avoid issues
like memory leaks, dangling pointers, and undefined behavior. The language itself does not provide built-in mechanisms
for checking if a memory location is allocated and set. It's important to follow best practices to avoid common pitfalls
related to dynamic memory management.
*/