Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Developing concise and quick Component Object Model (COM) objects is possible through the Active Template Library (ATL), a collection of template-based C++ classes. Memory management is a very crucial factor in software development. ATL consists of various memory management classes. These classes support heap, smart pointers, and other memory allocation routines.
In this article, we will learn about the memory management class- CComHeap and CComHeapPtr.
Memory Management Class- CComHeap and CComHeapPtr
CComHeap class uses the COM memory allocation functions to implement IAtlMemMgr.The IAtlMenMgr class serves as a memory manager's interface.
CComHeapPtr is a smart pointer class for managing heap pointers.
Smart pointers, used to help ensure that programmes are free of memory and resource leaks and are exception-safe, are a part of the Standard Library in modern C++.
CComHeap Class
This class uses the COM memory allocation functions to implement IAtlMemMgr.
The Syntax is as follows:
class CComHeap : public IAtlMemMgr
Note: You cannot use this class and the members of this class in Windows Runtime apps.
IATLMemMgr
The IAtlMemMgr interface is defined in the atlmem.h header file, which represents the generic memory-management model. The four primary functions defined on this interface provide most of the dynamic memory functions needed in regular applications. The four functions are:
Allocate: Contiguous space n bytes in size should be allocated as a reserve in a heap.
Free: To make it available for future allocation requests, Free takes a pointer to a memory block acquired from Allocate and returns it to the heap.
Reallocate: It is helpful when an allocated block isn't big enough to fit extra data and growing the existing block is more efficient than allocating a new bigger block and copying the contents.
Get Size: GetSize takes a pointer to a block allocated by Allocate and returns the block's current size in bytes.
Is it possible to create a heap pointer using CComHeapPtr Object?
Yes! We can create a heap pointer using the class object, but It is optional to use an existing CComHeapPtr object to generate the heap pointer. If so, control of the new pointer and resources passes to the new CComHeapPtr object.
What do you mean by CHeapPtrBase Class?
The CHeapPtrbase Class is a basic class of Active Template Library(ATL). Many smart heap pointer classes are built on the foundation of this class. One cannot use this class and the members of this class in Windows Runtime apps. The CHeapPtr class is the child of CHeapPtrBase Class.
What are LocalAlloc and GlobalAlloc functions?
To manipulate the heap, earlier versions of Windows used operations like LocalAlloc and GlobalAlloc; however, these techniques have since been deprecated. Applications now manage the heap and the data blocks included within the default heap using Win32 functions like HeapCreate, HeapAlloc, and HeapFree.
Conclusion
We have discussed the memory management classes- CComHeap and CComHeapPtr. These are the smart heap pointers classes in the Active Template Library. Smart pointers, used to help ensure that programmes are free of memory and resource leaks and are exception-safe, are a part of the Standard Library in modern C++.
We hope this blog has helped you. We recommend you visit our articles on different topics of ATL, such as