mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
Fix calls to malloc() with 2 arguments (#1377)
* Fix calls to malloc() with 2 arguments
This commit is contained in:
committed by
Sergey Zubkov
parent
f67e91d295
commit
ba2dbc5edf
@@ -15787,7 +15787,7 @@ Better:
|
||||
|
||||
void f(int n)
|
||||
{
|
||||
void* p = malloc(1, n);
|
||||
void* p = malloc(n);
|
||||
auto _ = finally([p] { free(p); });
|
||||
// ...
|
||||
}
|
||||
@@ -15891,7 +15891,7 @@ In such cases, "crashing" is simply leaving error handling to the next level of
|
||||
void f(int n)
|
||||
{
|
||||
// ...
|
||||
p = static_cast<X*>(malloc(n, X));
|
||||
p = static_cast<X*>(malloc(n * sizeof(X)));
|
||||
if (!p) abort(); // abort if memory is exhausted
|
||||
// ...
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user