Table of contents
1.
Introduction
2.
Memory Management Class- CComHeap and CComHeapPtr
3.
CComHeap Class
3.1.
IATLMemMgr
3.2.
Members
3.2.1.
Allocate
3.2.2.
Free
3.2.3.
Reallocate
3.2.4.
GetSize
4.
CComHeapPtr Class
4.1.
Members
4.1.1.
Public Constructor
5.
Frequently Asked Questions
5.1.
Is it possible to create a heap pointer using CComHeapPtr Object?
5.2.
What do you mean by CHeapPtrBase Class?
5.3.
What are LocalAlloc and GlobalAlloc functions?
6.
Conclusion
Last Updated: Mar 27, 2024
Hard

Memory Management Class- CComHeap and CComHeapPtr

Author Nidhi Kumari
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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

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.

 

The prototype is as follows:

__interface IAtlMemMgr {                        
public:                                         
    void* Allocate( size_t nBytes ) ;           
    void Free( void* p ) ;                      
    void* Reallocate( void* p, size_t nBytes ) ;
    size_t GetSize( void* p ) ;                 
};

 

Members

The members of the CComHeap Class are the four Public methods we have discussed above. Now let’s discuss each one of them in detail.

Allocate

The CComHeap::Allocate method allocates the contiguous block of memory.

Heap function used: CoTaskMemAlloc.

The syntax of this method is as follows:

virtual __declspec(allocator) void* Allocate(size_t nBytes) throw();

 

Parameters

nBytes: The required size of the memory block in bytes.

 

Return Value

A pointer to the first address of the recently allocated memory block is returned.

 

Free

To release a memory block that the memory management allocate method has allocated, call CComHeap:: Free function.

Heap function used: CoTaskMemFree.

The syntax of this method is as follows:

virtual void Free(void* p) throw();

 

Parameters

p: The pointer to the allocated memory.

 

Return Value

None

 

Reallocate

The CComHeap::Reallocate method reallocates the block of memory.

Heap function used: CoTaskMemRealloc.

The syntax of this method is as follows:

virtual __declspec(allocator) void* Reallocate(void* p, size_t nBytes) throw();

 

Parameters

p: The pointer to the allocated memory.

nBytes: The required size of the memory block in bytes.

 

Return Value

A pointer to the first address of the recently reallocated memory block is returned.

 

GetSize

The CComHeap::GetSize method returns the size of the allocated block of memory. 

Heap function used: IMalloc::GetSize.

The syntax of this method is as follows:

virtual size_t GetSize(void* p) throw();

 

Parameters

p: The pointer to the previously allocated memory.

 

Return Value

It gives back the allocated memory block's size in bytes.

CComHeapPtr Class

This class is a smart pointer to deal with the heap pointers. 

The syntax of this class is as follows:

template<typename T>
class CComHeapPtr : public CHeapPtr<T, CComAllocator>

 

Parameters

T: The type of object that will be stored on the heap.

Members

The members of the CComHeapPtr Class are Constructors.

To access this member, we need altbase.h header file.

Public Constructor

CComHeapPtr::CComHeapPtr is the public constructor of the CComHeapPtr class.

As a child of CHeapPtr, CComHeapPtr employs COM routines to allocate memory through the use of CComAllocator.

Inheritance Hierarchy

 

The syntax of the constructor is as follows:

CComHeapPtr() throw();
explicit CComHeapPtr(T* pData) throw();

 

Parameters

pData: It is a CComHeapPtr object in existence.

Frequently Asked Questions

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

🔥 ATL module classes.

🔥 Memory Hierarchy

🔥 Memory management class-CAutoPtrArray and CAutoPtrList.

🔥 Memory management class-CComGitPtr.

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Reading!!

Live masterclass