惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
D
Docker
Vercel News
Vercel News
IT之家
IT之家
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
腾讯CDC
Google DeepMind News
Google DeepMind News
I
InfoQ
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - Franky
The Cloudflare Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
T
Tenable Blog
P
Proofpoint News Feed
Recorded Future
Recorded Future
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
The Hacker News
The Hacker News
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
F
Full Disclosure
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
Project Zero
Project Zero

博客园 - 悠然小调

nothing.... Ogre中在SceneNode节点旁显示二维字的代码 博文阅读密码验证 - 博客园 最简单的智能指针原理 模板的特化 计算基类虚表指针在派生类中的偏移量 写一个内存拷贝函数 [转]来自 COM 经验的八个教训 COM本质论学习笔记(一)IDL winsock中select的作用 windows核心编程学习笔记(三)线程池(Thread Pooling) windows核心编程学习笔记(八)结构化异常处理(Structured Exception Handling) [转]亲密接触VC6.0编译器 windows核心编程学习笔记(七)DLL Injection and API Hooking windows核心编程学习笔记(五)内存映射文件 windows核心编程学习笔记(四)windows内存结构/虚拟内存/线程的堆栈 windows核心编程学习笔记(二)Wait For Kernel Object(s) windows核心编程学习笔记(一)使用Critical Section [转]筛选法求素数
windows核心编程学习笔记(五.续)堆
悠然小调 · 2008-03-03 · via 博客园 - 悠然小调

堆(Heap)和栈(Stack)不同,堆是给进程用的,用来存储各个线程申请的内存块。
不能同时在堆上进行Alloc操作,这就意味这如果2个线程同时执行new操作,那么一个可以执行,另一个要等到这个执行完毕才可以执行new——否则的话,可能返回同一个地址,而线程还傻乎乎的以为是不同的呢。因此,如果想获取高效率而不出现问题(当然还有其他原因),那么可以另外创建一个堆来使用。使用函数HeapCreate来创建一个堆。
    从堆中分配内存:HeapAlloc
    改变已分配内存大小:HeapReAlloc
    获取已分配内存块大小:HeapSize
    释放已分配内存块:HeapFree
    销毁堆:HeapDestroy
可是,此时使用new操作符,仍将在默认的堆上分配空间,我们需要在新的堆上来分配空间,此时需要重载对应类的new和delete操作符。

class CSomeClass 
{
private:
   
static HANDLE s_hHeap;
   
static UINT s_uNumAllocsInHeap;
   
// Other private data and member functions
public:
   
void* operator new (size_t size);
   
void operator delete (void* p);
   
// Other public data and member functions
};

HANDLE CSomeClass::s_hHeap 

= NULL;
UINT CSomeClass::s_uNumAllocsInHeap 
= 0;void* CSomeClass::operator new (size_t size)
{
   
if(s_hHeap == NULL)
   {
      
// Heap does not exist; create it.
      s_hHeap = HeapCreate(HEAP_NO_SERIALIZE, 00);if(s_hHeap == NULL)
         
return(NULL);
   }
   
// The heap exists for CSomeClass objects.
   void* p = HeapAlloc(s_hHeap, 0, size);if(p != NULL) 
   {
      
// Memory was allocated successfully; increment
      
// the count of CSomeClass objects in the heap.
      s_uNumAllocsInHeap++;
   }
// Return the address of the allocated CSomeClass object.
   return(p);
}
void CSomeClass::operator delete (void* p) 
{
   
if(HeapFree(s_hHeap, 0, p))
   {
      
// Object was deleted successfully.
      s_uNumAllocsInHeap--;
   }
if(s_uNumAllocsInHeap == 0)
   {
      
// If there are no more objects in the heap,
      
// destroy the heap.
      if(HeapDestroy(s_hHeap))
      {
         
//Set the heap handle to NULL so that the new operator
         
//will know to create a new heap if a new CSomeClass
         
//object is created.
         s_hHeap = NULL;
      }
   }
}

本章末尾讲解了其他堆操作函数,但用途似乎不大,可以选看。