Declare member variable on stack or heap
-
-
How is more efficient to have, a CMap member variable created on the stack, or on the heap, even the values of CMap are just pointers:
CMap m_MapContactInfo;
or
CMap* m_pMapContactInfo;
knowing that this CMap could contain thousands of elements ...
-
Probably best on the heap if it contains large amounts of data. You also need to consider scope, i.e. how long will the object need to remain in memory?
"how long will the object need to remain in memory?" As long as object that contain m_pMapContactInfo are alive ... I mean:
CMyObject::CMyObject()
{
m_pMapContactInfo = new CMap;
}and
CMyObject::~CMyObject()
{
if(NULL != m_pMapContactInfo)
delete m_pMapContactInfo;
} -
"how long will the object need to remain in memory?" As long as object that contain m_pMapContactInfo are alive ... I mean:
CMyObject::CMyObject()
{
m_pMapContactInfo = new CMap;
}and
CMyObject::~CMyObject()
{
if(NULL != m_pMapContactInfo)
delete m_pMapContactInfo;
} -
How is more efficient to have, a CMap member variable created on the stack, or on the heap, even the values of CMap are just pointers:
CMap m_MapContactInfo;
or
CMap* m_pMapContactInfo;
knowing that this CMap could contain thousands of elements ...
CMap class creates its content data in the heap. It is by design. So you can create your CMap instance on the stack and not worry!
-
CMap class creates its content data in the heap. It is by design. So you can create your CMap instance on the stack and not worry!
-
Flaviu2 wrote:
As long as object that contain m_pMapContactInfo are alive
Only you know how long that will be.
-
In fact, the m_pMapContactInfo are member variable into CView derived class ... so, as long the view are opened, the m_pMapContactInfo exist ... but what is matter how long the CMap object are exist ?
-
CMap class creates its content data in the heap. It is by design. So you can create your CMap instance on the stack and not worry!
i am still not having a perfect solution with both options Deep Cleaning Delhi Home Cleaning Delhi
-
i am still not having a perfect solution with both options Deep Cleaning Delhi Home Cleaning Delhi
Hometurph Indi wrote:
i am still not having a perfect solution with both options
Define perfect.:cool: