3 ms·
My guess would be that they lean on the fact that "malloc never fails" [1]. Under that model, new and malloc always give you a pointer you can use, it's just th
by twic 7y ago
My guess would be that they lean on the fact that "malloc never fails" [1]. Under that model, new and malloc always give you a pointer you can use, it's just that sometimes, using it will give you a segfault.
[1] https://scvalex.net/posts/6/ https://scvalex.net/posts/6/
- MaxBarraclough 7y agoThat blog post describes Linux's default 'memory overcommitment' behaviour. Oddly it fails to mention the term 'overcommit'. As the post says, malloc can return null even where overcommit is used; it's not true that malloc never fails when overcommit is enabled. Even if it were true that malloc/calloc/realloc/new/new[] could never fail on your OS+compiler, it doesn't seem wise to write code that relies on that non-standard guarantee. Poor robustness and portability.