Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22282

How can the assembly be the same if one program leaks and the other doesn't? [duplicate]

$
0
0

I do apologize in advance if I am missing something glaringly obvious.

I have two versions of the same program. One intentionally leaks and one does not:

#include <iostream>int main() {    int* y = new int;    *y = 32;    printf("Y is: %i", *y);    delete y; //y doesn't leak    return 0;}
#include <iostream>int main() {    int* y = new int;    *y = 32;    printf("Y is: %i", *y);    //delete y; y leaks    return 0;}

Please see:https://gcc.godbolt.org/z/oPrsoKx1E (no optimization)

https://gcc.godbolt.org/z/nTq9K7jY7 (-O3 optimization)

What is interesting is that in the -O3 optimization the two programs produce identical assembly.

With no optimization the first program calls:

call    operator delete(void*, unsigned long)

But in the optimized code they have the exact same assembly. Clearly, the second program leaks while the first doesn't.


Viewing all articles
Browse latest Browse all 22282

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>