繁体   English   中英

编译器忽略运算符的新分配

[英]compiler ignores operator new allocation

我在C ++中编写一个512位整数。 对于整数,我使用new关键字从堆中分配内存,但是编译器(MINGW上的g ++版本8.1)似乎错误地对其进行了优化。 即编译器命令是:

g++ -Wall -fexceptions -Og -g -fopenmp -std=c++14 -c main.cpp -o main.o

g++ -o bin\\Debug\\cs.exe obj\\Debug\\main.o -O0 -lgomp

码:

#include <iostream>
#include <cstdint>
#include <omp.h>

constexpr unsigned char arr_size = 16;
constexpr unsigned char arr_size_half = 8;
void exit(int);

struct uint512_t{
    uint32_t * bytes;
    uint512_t(uint32_t num){
        //The line below is either (wrongfully) ignored or (wrongfully) optimized out
        bytes = new(std::nothrow) uint32_t[arr_size];
        if(!bytes){
            std::cerr << "Error - not enough memory available.";
            exit(-1);
        }
        *bytes = num;
        for(uint32_t * ptr = bytes+1; ptr < ptr+16; ++ptr){
            //OS throws error 0xC0000005 (accessing unallocated memory) here
            *ptr = 0;
        }
    }
    uint512_t inline operator &(uint512_t &b){
        uint32_t* itera = bytes;
        uint32_t* iterb = b.bytes;
        uint512_t ret(0);
        uint32_t* iterret = ret.bytes;
        for(char i = 0; i < arr_size; ++i){
            *(iterret++) = *(itera++) & *(iterb++);
        }
        return ret;
    }

    uint512_t inline operator =(uint512_t &b){
        uint32_t * itera=bytes, *iterb=b.bytes;
        for(char i = 0; i < arr_size; ++i){
            *(itera++) = *(iterb++);
        }
        return *this;
    }
    uint512_t inline operator + (uint512_t &b){
        uint32_t * itera = bytes;
        uint32_t * iterb = b.bytes;
        uint64_t res = 0;
        uint512_t ret(0);
        uint32_t *p2ret = ret.bytes;
        uint32_t *p2res = 1+(uint32_t*)&res;
        //#pragma omp parallel for shared(p2ret, res, p2res, itera, iterb, ret) private(i, arr_size) schedule(auto)
        for(char i = 0; i < arr_size;++i){
            res = *p2res;
            res += *(itera++);
            res += *(iterb++);
            *(p2ret++) = (i<15) ? res+*(p2res) : res;
        }
        return ret;
    }
    uint512_t inline operator += (uint512_t &b){
        uint32_t * itera = bytes;
        uint32_t * iterb = b.bytes;
        uint64_t res = 0;
        uint512_t ret(0);
        uint32_t *p2ret = ret.bytes;
        uint32_t *p2res = 1+(uint32_t*)&res;
        //#pragma omp parallel for shared(p2ret, res, p2res, itera, iterb, ret) private(i, arr_size) schedule(auto)
        for(char i = 0; i < arr_size;++i){
            res = *p2res;
            res += *(itera++);
            res += *(iterb++);
            *(p2ret++) = (i<15) ? res+(*p2res) : res;
        }
        (*this) = ret;
        return *this;
    }
    //uint512_t inline operator * (uint512_t &b){
    //}
    ~uint512_t(){
        delete[] bytes;
    }
};

int main(void){
    uint512_t a(3);
}

错误在这一行,与new的优化无关:

for(uint32_t * ptr = bytes+1; ptr < ptr+16; ++ptr){
    *ptr = 0;
}

对于条件for是错误的。 ptr < ptr+16永远不会为假。 该循环将永远进行下去,最终您将取消引用无效的内存位置,因为ptr广告无限增加。


顺便说一下,允许编译器执行优化,但不允许更改程序的外观。 如果您的代码执行的是new ,则编译器可以确保可以在需要时保留new副作用(在这种情况下,当您访问数组时)就可以对其进行优化。

ptr < ptr+16始终为true。 循环是无限的,最终溢出了它写入的缓冲区。

简单的解决方案:值初始化数组,以便您不需要循环:

bytes = new(std::nothrow) uint32_t[arr_size]();
//                                          ^^

PS。 如果复制实例,则行为将是不确定的,因为该副本将指向相同的分配,并且两个实例都将尝试在析构函数中将其删除。

简单的解决方案:不要使用裸拥有的指针。 如果需要动态分配数组,请使用RAII容器,例如std::vector


PPS。 首先要仔细考虑是否需要动态分配(以及相关的开销)。 在许多情况下,512位是就位的相当安全的大小。

您正在访问数组。 最小的可复制示例为:

#include <cstdint>
int main() {
        uint32_t bytes[16];
        for(uint32_t * ptr = bytes + 1; ptr < ptr + 16; ++ptr){
            //OS throws error 0xC0000005 (accessing unallocated memory) here
            *ptr = 0;
        }
}

ptr < ptr + 16始终为true(也许除了溢出)。

附言:我尝试了您的解决方案,并且效果很好-

bytes = new(std::nothrow) uint32_t[arr_size];
    if(!bytes){
        std::cerr << "Error - not enough memory available.";
        exit(-1);
    }
    *bytes = num;
    auto ptrp16 = bytes+16;
    for(uint32_t * ptr = bytes+1;ptr < ptrp16 ; ++ptr){
        *ptr = 0;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM