Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么inptr的行为在不同平台之间有所不同?_C++_Algorithm_Pointers - Fatal编程技术网

C++ 为什么inptr的行为在不同平台之间有所不同?

C++ 为什么inptr的行为在不同平台之间有所不同?,c++,algorithm,pointers,C++,Algorithm,Pointers,我使用GCC 9.2编写了一些在上使用指针算术的代码。后来,我尝试在我的Windows机器上使用MinGW编译相同的代码 这是我在GodBolt上运行的代码的最低限度示例: #include <iostream> #include <memory> struct release { void operator()(void* data) const noexcept { free(data); } }; int main() {

我使用GCC 9.2编写了一些在上使用指针算术的代码。后来,我尝试在我的Windows机器上使用MinGW编译相同的代码

这是我在GodBolt上运行的代码的最低限度示例:

#include <iostream>
#include <memory>

struct release {
    void operator()(void* data) const noexcept {
        free(data);
    }
};

int main() {
    auto x = std::unique_ptr<float[], release>{ static_cast<float*>(aligned_alloc(100U * sizeof(float), 32U)) };

    if(!x.get() || reinterpret_cast<uintptr_t>(x.get()) % 32U != 0U)
        std::cout << "Failed to allocate or align memory\n";

    for(auto i = 0U; i < 100U; ++i)
        *reinterpret_cast<float*>(reinterpret_cast<intptr_t*>(x.get()) + i * sizeof(float)) = i;

    for(auto i = 0U; i < 100U; ++i)
        std::cout << *reinterpret_cast<float*>(reinterpret_cast<intptr_t*>(x.get()) + i * sizeof(float)) << std::endl;

}
#包括
#包括
结构释放{
void运算符()(void*数据)常量noexcept{
免费(数据);
}
};
int main(){
auto x=std::unique_ptr{static_cast(aligned_alloc(100U*sizeof(float),32U))};
如果(!x.get()| | reinterpret_cast(x.get())%32U!=0U)

std::cout
reinterpret\u cast
应该是
reinterpret\u cast
。你不是在指向
intptr\u t
的指针上操作。你只是直接在
intptr\u t
上操作。你在
if
语句中做得很好,但在
for
循环中搞砸了。你可以通过执行
x.get()[i]来避免这种废话而不是所有的铸造。我尝试将它转换为<代码> INTpRtTyt/C++ >而不是<代码> INTpRt**/Cuth>,但是当我在Windows上检查了代码< INTPrTrt> <代码>的定义时,它被定义为:<代码> TyPulfS.It64 INTrtRtt。能够容纳一个整数的整数pointer@Maki显然,
\uu int64
与64位窗口上的指针大小相同。这没有错。我并不是说这有什么问题。我只是不知道如何使用
intptr\u t
而不是
intptr\u t*
来解决我的问题。特别是因为我收到了一个指针从
unique\u ptr::get
函数中。好的,现在我明白你的意思了。我应该使用
intptr\u t
,而不是
intptr\u t*
。但是另一件事是我的分配被颠倒了。谢谢,这个问题已经解决了。现在我需要弄清楚为什么这不能解决我项目中的相同问题。。。