2 ms·
new, when the compiler can see the definition at build time and with known size, may compile down to as little as two x86 instructions without a call. That's no
by jeffbee 5y ago
new, when the compiler can see the definition at build time and with known size, may compile down to as little as two x86 instructions without a call. That's not generally true of malloc. C also lacks an equivalent of sized delete, which is very efficient. In C the implementation of free has to figure out what size ptr represented.
- remexre 5y agoIs that some sort of allocation elision where it's actually on the stack? Or more to the point, what are those two instructions?
- mariusor 5y ago> new, [...] may compile down to as little as two x86 instructions Yes, but when you have a million of allocation, that still means two million instructions. malloc/free allows a much easier path to allocating a large chunk of memory in which you can play around with your million structs having only the overhead of accessing pointers (and, of course, thinking about where to point them).
- kaba0 5y agoWith all due respect, that is trivial to do in C++, hell it will be actually readable and much safer than malloc(n*sizeof(something)) and then for looping over that to initialize values.