大佬教程收集整理的这篇文章主要介绍了c – 如何查找继承类的分配地址,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
完整的代码可在以下网址查看: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
更新:基于user396672的评论
我相信下面的代码解决了我的问题,但有些细节可能仍然是错误的.另外,我想避免使用模板参数来解析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
#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; }
以上是大佬教程为你收集整理的c – 如何查找继承类的分配地址全部内容,希望文章能够帮你解决c – 如何查找继承类的分配地址所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。