You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> warning C6308: 'realloc' may return null pointer: assigning a null pointer to \<variable>, which is passed as an argument to 'realloc', will cause the original memory block to be leaked
> 'realloc' may return null pointer: assigning a null pointer to '\**parameter-name*', which is passed as an argument to 'realloc', will cause the original memory block to be leaked
13
15
14
-
This warning indicates a memory leak that is the result of the incorrect use of a reallocation function. Heap reallocation functions do not free the passed buffer if reallocation is unsuccessful. To correct the defect, assign the result of the reallocation function to a temporary, and then replace the original pointer after successful reallocation.
16
+
## Description
17
+
18
+
This warning indicates a memory leak due to the unsafe use of a reallocation function. Heap reallocation functions do not free the passed buffer if reallocation is unsuccessful. To correct the issue, assign the result of the reallocation function to a temporary location, and then replace the original pointer after successful reallocation.
15
19
16
20
## Example
17
21
18
-
The following sample code generates this warning:
22
+
The following sample code generates this warning. This issue stems from the assignment of x to the return value from realloc. If realloc fails and returns a null pointer, then x will fail to be freed:
19
23
20
24
```cpp
21
25
#include<malloc.h>
@@ -34,7 +38,7 @@ void f( )
34
38
}
35
39
```
36
40
37
-
To correct this warning, use the following code:
41
+
To resolve the issue, you can create a temporary variable to store the return status of realloc. This allows you to free the previously allocated memory safely in the case of realloc failing:
38
42
39
43
```cpp
40
44
#include <malloc.h>
@@ -43,9 +47,7 @@ To correct this warning, use the following code:
43
47
void f()
44
48
{
45
49
char *x, *tmp;
46
-
47
50
x = (char *) malloc(10);
48
-
49
51
if (x != NULL)
50
52
{
51
53
tmp = (char *) realloc(x,512);
@@ -61,7 +63,7 @@ void f()
61
63
62
64
This warning might generate noise if there is a live alias to the buffer-to-be-reallocated at the time of the assignment of the result of the reallocation function.
63
65
64
-
To avoid these kinds of problems altogether, use the mechanisms that are provided by the C++ Standard Template Library (STL). These include [shared_ptr](../standard-library/shared-ptr-class.md), [unique_ptr](../standard-library/unique-ptr-class.md), and [vector](../standard-library/vector.md). For more information, see [Smart Pointers](../cpp/smart-pointers-modern-cpp.md) and [C++ Standard Library](../standard-library/cpp-standard-library-reference.md).
66
+
To avoid these kinds of issues altogether, you can use the mechanisms that are provided by the C++ Standard Template Library (STL). These include [shared_ptr](../standard-library/shared-ptr-class.md), [unique_ptr](../standard-library/unique-ptr-class.md), and [vector](../standard-library/vector.md). For more information, see [Smart Pointers](../cpp/smart-pointers-modern-cpp.md) and [C++ Standard Library](../standard-library/cpp-standard-library-reference.md).
0 commit comments