- 浏览: 399001 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15338首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1008RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 769It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 955As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1057typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 768There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 818we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 975As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 751There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 959Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 974As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 993As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1327in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 936class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 985template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1209There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 849the template has parameter, it ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1165In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 868A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1184You can overload the subscript ...
相关推荐
简述 C++ 中的 new 和 delete 的基本原理,并讲述了内存检测子系统的实现原理、实现中的技巧,并对内存泄漏检测的高级话题进行了讨论。解决了本人在内存泄露很多多问题(我也是down的别人的),觉得非常好,所以给...
使用部署要创建运算符,可以直接使用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 ...
新版启动calico所需镜像quay.io/tigera/operator:v1.20.3
安装calico网络组件(仅master节点),上传tigera-operator-3-26-1.yaml和custom-res
准备好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...
hbase hbck2修复工具hbase-operator-tools-1.0.0.1.0.0.0-618-bin.tar.gz,hbase1版本的hbck已经不支持修复命令,hbase2.1版本需要用这个新版的工具
本文将通过VC8.0的汇编代码来深入分析这些操作符的具体实现机制,特别是`new`、`new[]`、`delete`和`delete[]`之间的差异以及它们如何被正确地使用。 #### `new` 和 `new[]` `new` 和 `new[]` 用于动态分配内存,...
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 ...
在C++编程中,全局`new`和`delete`操作符是用于动态内存分配和释放的关键部分。有时候,为了满足特定需求,如内存池管理、内存跟踪、异常安全或者资源的特殊处理,开发者可能需要重写这些全局操作符。本文将深入探讨...
在C++编程中,自定义`operator new`和`operator delete`是实现特定内存管理策略的重要手段。它们允许程序员根据需求调整内存分配和释放的行为。本文将深入探讨这两个操作符的自定义规则、使用场景以及潜在的陷阱。 ...
cluster-operator.yml
在这个压缩包“cassandra-operator,apache-cassandra的kubernetes算子.zip”中,包含的是Cassandra-Operator的源代码,可能还有相关的文档和示例配置,其主要文件夹名为“cassandra-operator-master”。 1. **...
本文详细介绍了 C++ 中的 new,包括 new operator、operator new、placement new 三种形态的内容,并且详细解释了每一种形态的行为和用法,同时也提到了 delete 的相关知识点,希望对读者有所帮助。
在C++编程中,`new`操作符和`operator new`是两个密切相关但不完全相同的概念,它们在内存管理和对象创建方面各自承担着特定的角色。 `new`操作符是C++语言内置的关键字,用于在堆上动态分配内存并创建对象。当执行...
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...
“redis-operator-master”目录包含了项目的源代码和相关文档,供开发者研究和定制。其中,主要的代码文件包括: 1. `api/`: 定义了`RedisCluster`资源的结构和相关操作。 2. `controllers/`: 包含了Redis运算符的...