- 浏览: 389137 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15282首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 964RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 744It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 919As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1031typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 735There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 783we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 913As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 721There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 930Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 923As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 949As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1285in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 902class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 956template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1179There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 798the template has parameter, it ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1132In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 836A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1126You can overload the subscript ...
相关推荐
简述 C++ 中的 new 和 delete 的基本原理,并讲述了内存检测子系统的实现原理、实现中的技巧,并对内存泄漏检测的高级话题进行了讨论。解决了本人在内存泄露很多多问题(我也是down的别人的),觉得非常好,所以给...
chaosblade-operator镜像
使用部署要创建运算符,可以直接使用kubectl创建它: kubectl create -f https://raw.githubusercontent.com/spotahome/redis-operator/master/example/operator/all-redis-operator-resources.yaml这将创建一个名为...
// has been causing me (and users of CStdString) problems for // years in some relatively rare conversions. I had reversed // two length arguments. // // 2003-NOV-24 - Thanks to a bunch ...
准备好opentelemetry-operator部署后,创建一个OpenTelemetry Collector(otelcol)实例,例如: $ kubectl apply -f - << EOF apiVersion: opentelemetry.io/v1alpha1 kind: OpenTelemetryCollector metada
从头开始初始化操作员代码 $ cd $GOPATH/src/github.com/onuryilmaz/$operator-sdk new k8s-operator-example --api-version=k8s.packt.com/v1 --kind=WeatherReport建立并推动 $ operator-sdk build onuryilmaz/k8s...
新版启动calico所需镜像quay.io/tigera/operator:v1.20.3
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/crds/jaegertracing.io_jaegers_crd.yaml kubectl create -n observability -f ...
cluster-operator.yml
prometheus-operator相关文件
kubectl apply -f https://raw.githubusercontent.com/FoundationDB/fdb-kubernetes-operator/master/config/crd/bases/apps.foundationdb.org_foundationdbclusters.yaml kubectl apply -f ...
What separates this title from the rest of the pack is that it jumps right in with samples that take advantage of the Standard C++ of today–from streams to built-in container classes, such as vectors...
flink-kubernetes-operator-1.7.0-src.tgz,flink-kubernetes-operator-1.7.0-src.tgz,flink-kubernetes-operator-1.7.0-src.tgz,flink-kubernetes-operator-1.7.0-src.tgz
Abstract— This paper investigates and proposes a framework for the efficient integration of functionalities for dynamic spectrum use (e.g., spectrum aggregation) and cooperative radio resource ...
安装calico网络组件(仅master节点),上传tigera-operator-3-26-1.yaml和custom-res
new operator/delete operator就是new和delete操作符,而operator new/operator delete是函数。 new operator(1)调用operator new分配足够的空间,并调用相关对象的构造函数(2)不可以被重载 operator new(1)只...
可以将其视为operator-sdk但适用于脚本。 Shell-operator用作支持Helm图表和值存储的更高级的基础。 Shell运算符提供: 轻松管理Kubernetes集群:使用Ops熟悉的工具。 它可以是bash,python,kubectl等。 ...
kubevirt所需yaml文件
mysql-operator-trunk
请查看,以了解MPI Operator及其行业采用情况。 安装 您可以通过运行以下命令以默认设置部署操作员: git clone https://github.com/kubeflow/mpi-operator cd mpi-operator kubectl create -f deploy/v1alpha2/...