C++在 x64 上崩溃

发布于 2025-01-14 01:49:34 字数 1398 浏览 3 评论 0原文

我必须以我的方式进行一点 C++ 反射,为类的每个属性创建一个指针(我创建了一个工具来帮助我生成相应的 C++ 代码),但令人惊讶的是,在 x86 模式上构建工作正常,但在 x64 上模式就崩溃了,我不知道为什么!这是我的代码。

Product.h 文件

    class Product
    {
    public:
        int ID;
        std::string  Designation;
    };
  class Property
    {
    public:
        std::string Name;
        int Shift;
    };
    class ProductSchema
    {
    private: 
        ProductSchema();
    public: 
        static ProductSchema* Default();
        ProductSchema(const ProductSchema& other) = delete;
        Property ID;
        Property Designation;
        Property Prix;
    };

Product.cpp 文件

ProductSchema::ProductSchema()
{  
    Product* p = new Product(); 
    ID.Name = "ID";
    ID.Shift = (int)(int*)&p->ID - (int)p;    

    Designation.Name = "Designation";
    Designation.Shift = (int)(int*)&p->Designation - (int)p;
}
ProductSchema* ProductSchema::Default()
{
    static ProductSchema* instance_;
    if (instance_ == nullptr)
        instance_ = new ProductSchema;
    return instance_;
}

main.h 文件

 int main()
    {     
        for (int i = 0; i < 10000; i++)
        {
            Product* p = new Product();
            int* pID = (int*)((unsigned long int)p + ProductSchema::Default()->ID.Shift);
            *pID = i; // <--- error here 
        } 
    }

I have to do a little c++ reflection in my way, by creating a pointer for each property of my class(I have create a tool to help me generate corresponding c++ code), but surprise, Building on x86 mode worked fine, but on x64 mode it's crashed, I have no idea why! here is my code.

Product.h File

    class Product
    {
    public:
        int ID;
        std::string  Designation;
    };
  class Property
    {
    public:
        std::string Name;
        int Shift;
    };
    class ProductSchema
    {
    private: 
        ProductSchema();
    public: 
        static ProductSchema* Default();
        ProductSchema(const ProductSchema& other) = delete;
        Property ID;
        Property Designation;
        Property Prix;
    };

Product.cpp File

ProductSchema::ProductSchema()
{  
    Product* p = new Product(); 
    ID.Name = "ID";
    ID.Shift = (int)(int*)&p->ID - (int)p;    

    Designation.Name = "Designation";
    Designation.Shift = (int)(int*)&p->Designation - (int)p;
}
ProductSchema* ProductSchema::Default()
{
    static ProductSchema* instance_;
    if (instance_ == nullptr)
        instance_ = new ProductSchema;
    return instance_;
}

main.h file

 int main()
    {     
        for (int i = 0; i < 10000; i++)
        {
            Product* p = new Product();
            int* pID = (int*)((unsigned long int)p + ProductSchema::Default()->ID.Shift);
            *pID = i; // <--- error here 
        } 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2025-01-21 01:49:34

您的 ProductSchema 类和 main() 正在泄漏它们new 的对象。

您不需要在运行时创建对象来计算其成员的偏移量,您可以使用 offsetof() 在编译时进行。

不要使用 intunsigned long 对指针执行计算。不保证它们足够大。请改用(u)intptr_t

您的单例在使用它之前没有初始化它的 instance_ 指针。它根本不需要使用动态内存。

试试这个:

class Product
{
public:
    int ID;
    std::string Designation;
};
 
struct Property
{
    std::string Name;
    int Shift;
};

class ProductSchema
{
private: 
    ProductSchema();
public: 
    static ProductSchema& Default();
    ProductSchema(const ProductSchema& other) = delete;
    Property ID;
    Property Designation;
    Property Prix;
};
ProductSchema::ProductSchema()
{  
    ID.Name = "ID";
    ID.Shift = offsetof(Product, ID);    

    Designation.Name = "Designation";
    Designation.Shift = offsetof(Product, Designation);
}

ProductSchema& ProductSchema::Default()
{
    static ProductSchema instance_;
    return instance_;
}
int main()
{     
    for (int i = 0; i < 10000; i++)
    {
        Product p;
        int* pID = reinterpret_cast<int*>(reinterpret_cast<uintptr_t>(&p) + ProductSchema::Default().ID.Shift);
        *pID = i;
    } 
}

在线演示

Your ProductSchema class, and your main(), are leaking the objects they new.

You don't need to create an object at runtime to calculate offsets to its members, you can use offsetof() at compile-time instead.

Don't use int or unsigned long to perform calculations on pointers. They are not guaranteed to be large enough. Use (u)intptr_t instead.

Your singleton is not initializing its instance_ pointer before using it. It does not need to use dynamic memory at all.

Try this instead:

class Product
{
public:
    int ID;
    std::string Designation;
};
 
struct Property
{
    std::string Name;
    int Shift;
};

class ProductSchema
{
private: 
    ProductSchema();
public: 
    static ProductSchema& Default();
    ProductSchema(const ProductSchema& other) = delete;
    Property ID;
    Property Designation;
    Property Prix;
};
ProductSchema::ProductSchema()
{  
    ID.Name = "ID";
    ID.Shift = offsetof(Product, ID);    

    Designation.Name = "Designation";
    Designation.Shift = offsetof(Product, Designation);
}

ProductSchema& ProductSchema::Default()
{
    static ProductSchema instance_;
    return instance_;
}
int main()
{     
    for (int i = 0; i < 10000; i++)
    {
        Product p;
        int* pID = reinterpret_cast<int*>(reinterpret_cast<uintptr_t>(&p) + ProductSchema::Default().ID.Shift);
        *pID = i;
    } 
}

Online Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文