如何查找继承类的分配地址
How to find the allocation address of inherited classes
我尝试创建几个通用的create
/delete
函数,这些函数用于嵌入式环境,其中正常的new
/delete
函数不可用,并且在分配内存时必须使用module_id。
完整的代码将在以下位置进行审查: https://codereview.stackexchange.com/questions/33858/implementing-create-and-destroy-functions-to-replace-new-and-delete-oper
当使用多个固有类时,我发现代码中存在问题:
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <utility>
#include <new>
using namespace std;
template<typename T_, typename ...Args>
T_ *create(uint32_t module_id, Args&&... args) {
// use module_id
T_ *p = (T_ *)calloc(1, sizeof(T_));
std::cout << "calloc: " << sizeof(T_) << " -> " << (void *)p << std::endl;
if (p)
new (p) T_(forward<Args>(args)...);
return p;
}
template<typename T_>
void destroy(T_ *t) {
if (!t)
return;
t->~T_();
std::cout << "free: " << (void *)t << std::endl;
free(t);
}
struct Foo {
int i[128];
virtual ~Foo() { }
};
struct Bar {
int j[128];
virtual ~Bar() { }
};
struct MyClass : public Foo, public Bar {
int k[128];
virtual ~MyClass() { }
};
#define MODULE_ID 42
int main() {
MyClass *myclass = create<MyClass>(MODULE_ID);
Bar *bar = myclass;
// Error bar != myclass
destroy(bar);
}
问题:如何修复/解决方法?该解决方案必须在带有gcc的Linux上运行,并且最好在带有clang的Linux上运行。
更新:基于用户396672的评论
我相信下面的代码解决了我的问题,但一些细节可能仍然是错误的。另外,我想避免使用模板参数来解析module_id
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <utility>
#include <new>
using namespace std;
template<unsigned ID, typename T>
struct MyObjectWrapper : public T {
template<typename ...Args>
MyObjectWrapper(Args&&... args) : T(forward<Args>(args)...) { }
void* operator new(std::size_t count) {
void *p = calloc(1, sizeof(MyObjectWrapper<ID, T>));
std::cout << "calloc: " << ID << " " <<
sizeof(MyObjectWrapper<ID, T>) << " -> " << (void *)p << std::endl;
return p;
}
void operator delete(void *p) {
std::cout << "free: " << p << std::endl;
free(p);
}
};
template<unsigned ID, typename T_, typename ...Args>
T_ *create(Args&&... args) {
return static_cast<T_ *>(new MyObjectWrapper<ID, T_>(
forward<Args>(args)...));
}
template<typename T_>
void destroy(T_ *t) {
delete /*static_cast<MyObjectWrapper<0, T_> *>*/(t);
}
struct Foo {
int i[128];
virtual ~Foo() { }
};
struct Bar {
int j[128];
virtual ~Bar() { }
};
struct MyClass : public Foo, public Bar {
int k[128];
~MyClass() { }
};
#define MODULE_ID 42
int main() {
MyClass *myclass = create<MODULE_ID, MyClass>();
Bar *bar = myclass;
// Error bar != myclass
destroy(bar);
}
问题1:这是否正确
问题2:这能做得更优雅吗?
问题 3:我是否可以避免将module_id作为模板参数传递,我可能会使用 full 来允许变量作为module_id
问题 4:MyObjectWrapper 对象是否需要虚拟构造函数?我相信不需要
我建议创建一个基类,其中包含带有附加参数(模块 id)的运算符 new。然后简单地从中继承你的类
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <utility>
#include <new>
using namespace std;
struct SpeciallyAllocated
{
// operator new with additional parameter:
void* operator new(size_t sz, uint32_t mod_id)
{
cout<<" module_id alloacted:"<<mod_id;
return calloc(1,sz);
}
// matching delete (actually is not invoked w/o exceptions):
void operator delete(void* p, uint32_t mod_id){}
// this one will be usually invoked:
void operator delete(void* p){cout<<" object deleted"; free(p);}
private: // prohibit (for safety reasons) other forms of new-delete operators:
void* operator new(size_t sz);
void* operator new[](size_t sz);
void operator delete[](void* p);
};
struct Foo: public SpeciallyAllocated{
int i[128];
virtual ~Foo() { cout<< " Foo destructed"; }
};
struct Bar: public SpeciallyAllocated {
int j[128];
virtual ~Bar() {{ cout<< " Bar destructed"; } }
};
struct MyClass : public Foo, public Bar {
int k[128];
virtual ~MyClass() { cout<< " MyClass destructed"; }
};
#define MODULE_ID 42
int main() {
MyClass *myclass = new(MODULE_ID) MyClass;
Bar *bar = myclass;
delete bar;
}
如果可以使用智能指针而不是原始指针,则可以传递自定义删除器。
在此示例中,我使用了std::shared_ptr
,但是很容易将其更改为std::unique_ptr
。
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <utility>
#include <new>
#include <memory>
using namespace std;
template<typename T_>
void destroy(T_ *t) {
if (!t)
return;
t->~T_();
std::cout << "free: " << (void *)t << std::endl;
free(t);
}
template<typename T_, typename ...Args>
std::shared_ptr<T_> create(uint32_t module_id, Args&&... args) {
// use module_id
T_ *p = (T_ *)calloc(1, sizeof(T_));
std::cout << "calloc: " << sizeof(T_) << " -> " << (void *)p << std::endl;
if (p)
new (p) T_(forward<Args>(args)...);
return std::shared_ptr<T_>(p,destroy<T_>);
}
struct Foo {
int i[128];
virtual ~Foo() { }
};
struct Bar {
int j[128];
virtual ~Bar() { }
};
struct MyClass : public Foo, public Bar {
int k[128];
virtual ~MyClass() { }
};
#define MODULE_ID 42
int main() {
auto myclass = create<MyClass>(MODULE_ID);
Bar *bar = myclass.get();
}
如果不允许这样做,我认为您创建的 CRTP 包装器没有任何问题。
这可能有点偏离主题,但您也可以考虑完全执行您想要执行的操作的现成组件。
为什么要自己实现它,然后不保留任何接口?所有 std 容器都允许您将自定义分配器传递给它们。如果你想像std::vector
一样使用东西,那么使用分配器概念可能会派上用场。
此外,boost已经提供了池,它允许您进行池分配,您可以使用单例池并使用固定大小的自定义分配器。为了处理不同的对象大小,通常使用不同的池来避免碎片。
还有一些内存分配器,如 jemalloc 和 tcmalloc,它们试图通过按大小分隔分配来避免锁定和碎片,因此即使使用它们不是一种选择,研究该代码也可能很有趣。
关于您的代码:我认为代码工作正常,但对界面的更改将大大提高性能。此外,您将malloc
替换为calloc
使用单独的对象(如 allocator
),该对象保存相同大小的所有实例的内存。在重载的新运算符中请求该对象内存。因为您使用的是嵌入式系统,所以您应该知道代码最多需要多少实例,并且您应该能够根据该大小调整分配器的大小。您需要某种方法来跟踪allocator
中的哪些对象实际正在使用,哪些未使用。位向量可能足以保留此信息。如果您可以阅读德语 Scott Meyers 幻灯片的嵌入式C++(免费)可能会有所帮助。它们也有英文版本(不是免费的)。
- 将数组的地址分配给变量并删除
- 将地址分配给本地指针后,公共对象的变量将消失
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 在什么情况下,两个堆栈分配的结构对象的 this 点指向同一个地址?
- 如果 const 不分配内存,为什么我可以获取 const 的地址?
- 当我在 C++ 中将派生类的指针分配给指针时,地址会更改
- LLVM 传递以在特定地址分配内存
- 是否可以仅通过将分配的指针地址存储在C++中来分析内存?
- 给定特定内存地址的数组的动态内存分配
- 堆栈分配的类类型.为什么两个 ID 实例的地址相同?
- 获取指针的地址会为其分配一个值
- 任务管理器 - C/C++ 应用程序 - 分配的地址空间与已用内存
- 如何按顺序或在指定的地址分配内存?
- 我可以直接为指针分配地址吗?如果是,如何做到这一点
- 是否可以在运行时为宏分配地址
- 为迭代器分配地址
- 如何查找继承类的分配地址
- 如何在 c++ 中为 boost::shared_ptr 分配地址
- 为指针分配地址
- 使用 'memcpy()' 为指针分配地址