3 ms·
It can be turned of though. Compiler optimizations performed around memory allocations are weird sometimes.
by turminal 5y ago
It can be turned of though. Compiler optimizations performed around memory allocations are weird sometimes.
- geofft 5y agoYes, but this is by far the least weird type of optimization, and the one you'd least want to turn off. It's the sort taught in undergraduate compiler design classes. Here's a piece of code that would go slower if you turned it off: for (i = 0; i < 1000; i++) { struct foobar *ptr = malloc(sizeof(struct foobar)); ptr->this = a[i]; ptr->that = b[i]; if (ptr->this > 0) { do_stuff(ptr); } free(ptr); } You only need to write to ptr->that if the condition succeeds. (And frankly you only need to do the allocation at all if a[i] > 0.) Compilers that don't do this get rejected for compilers that do.