mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Fix calls to malloc() with 2 arguments (#1377)
* Fix calls to malloc() with 2 arguments
This commit is contained in:
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
|
||||
// ...
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user