Are they not allowed to do so?
Maybe, but optimization not done in this case may be due to corner functional differences.
If 150 bytes of allocatable memory remain,
data = malloc(100); data = realloc(data, 200);
returns NULL
with 100 bytes consumed (and leaked) and 50 remain.
data = malloc(200);
returns NULL
with 0 bytes consumed (none leaked) and 150 remain.
Different functionality in this narrow case may prevent optimization.
Are compilers allowed to optimize-out realloc?
Perhaps - I would expect it is allowed. Yet it may not be worth the effect to enhance the compiler to determine when it can.
Successful malloc(n); ... realloc(p, 2*n)
differs from malloc(2*n);
when ...
may have set some of the memory.
It might be beyond that compiler's design to ensure ...
, even if empty code, did not set any memory.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…