0
点赞
收藏
分享

微信扫一扫

GCC compiling issue [Werror=alloc-size-larger-than=]

深夜瞎琢磨 2022-03-24 阅读 75
gnuc++linux
    template<class _Ty, class... _Types>
    static _Ty* NewArray(int32_t size)
    {
        _Ty* ptr = new (std::nothrow) _Ty[size]();
        return ptr;
    }

 error: argument 1 range [18446744071562067968, 18446744073709551615] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]
      _Ty* ptr = new (std::nothrow) _Ty[numElements]();

85783 – alloc-size-larger-than fires incorrectly with new[] and can't be disabled says that 

 The code was added in r190546 as a solution to prevent unsigned wrapping (when array new expression must compute the amount of space to allocate as a product of the number of elements and element size).  When the replacement operator new[] is inlined the excessive argument is propagated to malloc() and ultimately triggers the warning.

To fix it, change it to static _Ty* NewArray(size_t size).

C++ new[] operator defintion

throwing (1)
void* operator new[] (std::size_t size);
nothrow (2)
void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;
placement (3)
void* operator new[] (std::size_t size, void* ptr) noexcept;

举报

相关推荐

0 条评论