Home > Backend Development > C++ > Can a C Compiler Optimize Away `new` Operator Calls?

Can a C Compiler Optimize Away `new` Operator Calls?

DDD
Release: 2024-12-02 08:49:09
Original
893 people have browsed it

Can a C   Compiler Optimize Away `new` Operator Calls?

Can a Compiler Optimize Out Heap Memory Allocations?

Consider the following simple C code that allocates memory using the new operator:

int main()
{
    int* mem = new int[100];
    return 0;
}
Copy after login

Can the compiler optimize out the new call, thus avoiding the dynamic memory allocation?

Compiler Behavior

Different compilers behave differently in this situation. g and Visual Studio 2015 do not optimize out the new call, while clang does, as observed in tests with full optimizations enabled.

Compiler Optimization Rationale

According to N3664: Clarifying Memory Allocation, which later became part of C 14, the compiler is allowed to optimize around memory allocations. This optimization is based on the assumption that the new call does not have observable side effects.

As-If Rule

However, the as-if rule in the draft C standard section 1.9 requires conforming implementations to emulate the observable behavior of the abstract machine. Throwing an exception from new would have observable side effects, so it could be argued that the compiler is not allowed to optimize out the new call.

Implementation Detail

On the other hand, it could be argued that the decision of when to throw an exception from new is an implementation detail. Clang could potentially determine that the allocation would not cause an exception and therefore elide the new call without violating the as-if rule.

Non-Throwing Allocations

Using the non-throwing version of new, new (std::nothrow) int[100], still allows clang to optimize out the allocation. This is because clang may be able to prove that there is no global replacement operator new that could cause observable behavior.

Aggressive Optimization

In earlier versions of clang, even more aggressive optimizations were made, as seen in this code:

#include <cstddef>

extern void* operator new(std::size_t n);

template<typename T>
T* create() { return new T(); }

int main() {
    auto result = 0;
    for (auto i = 0; i < 1000000; ++i) {
        result += (create<int>() != nullptr);
    }
    return result;
}
Copy after login

This code was optimized to:

main:                                   # @main
    movl    00000, %eax          # imm = 0xF4240
    ret
Copy after login

Effectively, the entire loop was optimized away. Later versions of clang do not perform such aggressive optimizations.

The above is the detailed content of Can a C Compiler Optimize Away `new` Operator Calls?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template