3 ms·
Another potential problem is that "new" can throw an exception.
by amelius 8y ago
Another potential problem is that "new" can throw an exception.
- monocasa 8y agoIn practice they don't on a lot systems. Last time I checked glibc baked in Linux's overcommit by default nonsense, just throws up it's hands and says 'malloc never fails'.
- gpderetta 8y agoThere are 4 options: 1) allocation can't fail for whatever reason. So new won't ever throw. 2) allocation can fail but you do not care to handle it. Your application will abort on an exception which is the best scenario. 3) allocation can fail and you want to handle it non locally (for example in a constructor). Exceptions work well in this scenario. 4) allocation can fail and you want to handle it locally: new(nothrow) is your friend. Remember to check for '!= nullptr'.