Skip to content

Missed optimization: malloc + memcpy + free => realloc #128587

@Kmeakin

Description

@Kmeakin

https://godbolt.org/z/YWxW7h8sP

#include <cstdlib>
#include <cstring>
#include <memory>

extern "C" {
auto src1(void* p1, size_t old_len, size_t new_len) -> void* {
    void* p2 = malloc(new_len);
    memcpy(p2, p1, old_len);
    free(p1);
    return p2;
}

auto tgt1(void* p1, size_t old_len, size_t new_len) -> void* {
    return realloc(p1, new_len);
}

}

alive proof: https://alive2.llvm.org/ce/z/RwPKVk

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions