C++在 x64 上崩溃
我必须以我的方式进行一点 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
ProductSchema
类和main()
正在泄漏它们new
的对象。您不需要在运行时创建对象来计算其成员的偏移量,您可以使用
offsetof()
在编译时进行。不要使用
int
或unsigned long
对指针执行计算。不保证它们足够大。请改用(u)intptr_t
。您的单例在使用它之前没有初始化它的
instance_
指针。它根本不需要使用动态内存。试试这个:
在线演示
Your
ProductSchema
class, and yourmain()
, are leaking the objects theynew
.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
orunsigned 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:
Online Demo