- 浏览: 408593 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
The operator new and delete operator can overloaded to achieve some customed memory management strategy, suppose we have a Sscreen object, and we want to allocate a chunk of memory instead of allocate each at a time when call the new operator to improve memory usage effeciency. Here is what we can do .
suppose we have the following members.
/** * file * Screen_Alloc.h * * description: * This is the code that shows you how to overload the allocation */ class Screen { public: void * operator new(size_t ); void operator delete(void *, size_t); private: Screen * next; static Screen *freeStore; static const int screenChunk; protected: };
We uses a static member to hold the free list pointer, and the screenChunk side control how much bunks we are going to allocate in one go.
and below is the implementation code.
/** * file * Screen_Alloc.cpp * * description: * This is the code that shows you how to overload the allocation */ #include "stdafx.h" #include "Screen_Alloc.h" // static member are initialized within program text files, no header files Screen * Screen::freeStore = 0; const int Screen::screenChunk = 24; void *Screen::operator new(size_t size) { Screen *p; if (freeStore == 0) { size_t chunk = screenChunk * size; // the size is populated by the compiler and is the size of Screen freeStore = p = reinterpret_cast<Screen *>(new char[chunk]); // now thread the memory allocated for (; p != &freeStore[screenChunk - 1]; ++p) { p->next = p + 1; } p-> next = 0; p = freeStore; freeStore = freeStore->next; return p; } } /** * the delete expression * delete ptr * is equivalent to * Screen::~Screen(ptr); * Screen::operator delete(ptr, sizeof(*ptr); */ void Screen::operator delete (void *p, size_t) { // inser the 'deleted' object back // into the free list (static_cast<Screen *> (p) )-> next = freeStore; freeStore = static_cast<Screen*>(p); }
Array operator new[] and operator delete[]
when you override the default operator new and operator delete,you may/may not defined the array operator [] and the array delete operator []...
void * operator new[](size_t); void operator delete[](void *, size_t);
to invoke the new[] operator you do this
Screen* screenptr = new Screen[10];
what it does, first allocate the size enough to hold the array, and for each of the element, it calles the default constructor to initialize the objct, and when this is all done, return the pointer to the caller.
to call the delete[] operator you can do this:
delete[] screenptr;// screenptr point to the Screen array
what it does is
first call the destructors iteratively, and then deallocate the memory of the array;
A common mistake is to call
delete screenptr;
it may deallocate the correct number of memory , but only the first objec't destructor will likely be called.
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15382首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1047RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 798It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 1003As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1090typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 814There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 856we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 1033As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 781There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 985Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 1006As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 1022As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1367in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 970class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 1009template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1233There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 885the template has parameter, it ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1195In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 901A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1234You can overload the subscript ...
相关推荐
嵌入式八股文面试题库资料知识宝典-华为的面试试题.zip
训练导控系统设计.pdf
嵌入式八股文面试题库资料知识宝典-网络编程.zip
人脸转正GAN模型的高效压缩.pdf
少儿编程scratch项目源代码文件案例素材-几何冲刺 转瞬即逝.zip
少儿编程scratch项目源代码文件案例素材-鸡蛋.zip
嵌入式系统_USB设备枚举与HID通信_CH559单片机USB主机键盘鼠标复合设备控制_基于CH559单片机的USB主机模式设备枚举与键盘鼠标数据收发系统支持复合设备识别与HID
嵌入式八股文面试题库资料知识宝典-linux常见面试题.zip
面向智慧工地的压力机在线数据的预警应用开发.pdf
基于Unity3D的鱼类运动行为可视化研究.pdf
少儿编程scratch项目源代码文件案例素材-霍格沃茨魔法学校.zip
少儿编程scratch项目源代码文件案例素材-金币冲刺.zip
内容概要:本文深入探讨了HarmonyOS编译构建子系统的作用及其技术细节。作为鸿蒙操作系统背后的关键技术之一,编译构建子系统通过GN和Ninja工具实现了高效的源代码到机器代码的转换,确保了系统的稳定性和性能优化。该系统不仅支持多系统版本构建、芯片厂商定制,还具备强大的调试与维护能力。其高效编译速度、灵活性和可扩展性使其在华为设备和其他智能终端中发挥了重要作用。文章还比较了HarmonyOS编译构建子系统与安卓和iOS编译系统的异同,并展望了其未来的发展趋势和技术演进方向。; 适合人群:对操作系统底层技术感兴趣的开发者、工程师和技术爱好者。; 使用场景及目标:①了解HarmonyOS编译构建子系统的基本概念和工作原理;②掌握其在不同设备上的应用和优化策略;③对比HarmonyOS与安卓、iOS编译系统的差异;④探索其未来发展方向和技术演进路径。; 其他说明:本文详细介绍了HarmonyOS编译构建子系统的架构设计、核心功能和实际应用案例,强调了其在万物互联时代的重要性和潜力。阅读时建议重点关注编译构建子系统的独特优势及其对鸿蒙生态系统的深远影响。
嵌入式八股文面试题库资料知识宝典-奇虎360 2015校园招聘C++研发工程师笔试题.zip
嵌入式八股文面试题库资料知识宝典-腾讯2014校园招聘C语言笔试题(附答案).zip
双种群变异策略改进RWCE算法优化换热网络.pdf
内容概要:本文详细介绍了基于瞬时无功功率理论的三电平有源电力滤波器(APF)仿真研究。主要内容涵盖并联型APF的工作原理、三相三电平NPC结构、谐波检测方法(ipiq)、双闭环控制策略(电压外环+电流内环PI控制)以及SVPWM矢量调制技术。仿真结果显示,在APF投入前后,电网电流THD从21.9%降至3.77%,显著提高了电能质量。 适用人群:从事电力系统研究、电力电子技术开发的专业人士,尤其是对有源电力滤波器及其仿真感兴趣的工程师和技术人员。 使用场景及目标:适用于需要解决电力系统中谐波污染和无功补偿问题的研究项目。目标是通过仿真验证APF的有效性和可行性,优化电力系统的电能质量。 其他说明:文中提到的仿真模型涉及多个关键模块,如三相交流电压模块、非线性负载、信号采集模块、LC滤波器模块等,这些模块的设计和协同工作对于实现良好的谐波抑制和无功补偿至关重要。
内容概要:本文探讨了在工业自动化和物联网交汇背景下,构建OPC DA转MQTT网关软件的需求及其具体实现方法。文中详细介绍了如何利用Python编程语言及相关库(如OpenOPC用于读取OPC DA数据,paho-mqtt用于MQTT消息传递),完成从OPC DA数据解析、格式转换到最终通过MQTT协议发布数据的关键步骤。此外,还讨论了针对不良网络环境下数据传输优化措施以及后续测试验证过程。 适合人群:从事工业自动化系统集成、物联网项目开发的技术人员,特别是那些希望提升跨协议数据交换能力的专业人士。 使用场景及目标:适用于需要在不同通信协议间建立高效稳定的数据通道的应用场合,比如制造业生产线监控、远程设备管理等。主要目的是克服传统有线网络限制,实现在不稳定无线网络条件下仍能保持良好性能的数据传输。 其他说明:文中提供了具体的代码片段帮助理解整个流程,并强调了实际部署过程中可能遇到的问题及解决方案。
基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档~ 基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C#实现的检测小说章节的重复、缺失、广告等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档
少儿编程scratch项目源代码文件案例素材-火柴人终极之战.zip