Are compilers allowed to optimize out realloc?
问题 I came across a situation where it would be useful to have unnecessary calls to realloc being optimized out. However, it seems like neither Clang nor GCC do such a thing (Compiler Explorer (godbolt.org)) - although I see optimizations being made with multiple calls to malloc . The example: void *myfunc() { void *data; data = malloc(100); data = realloc(data, 200); return data; } I expected it to be optimized to something like the following: void *myfunc() { return malloc(200); } Why is