Ouch! Exception During Realloc of Dynamic Array of Smart Pointers

Started by
20 comments, last by Raeldor 18 years, 7 months ago
I seem to be getting a heap error from the C run time library when trying to do a re-alloc on a section of memory that holds an array of smart pointers. Can anyone tell me what effect this should have on my smart pointers? I assumed that it would just do some kind of memory copy and that none of my smart pointer constructor/destructor logic should get called, and that everything would be ok (just in a different location). Is there something else going on here I am unaware of? Thanks
Advertisement
Was the memory allocated by malloc, calloc or realloc? Are the smart pointers POD types? If the answer to either of these questions is no then you shouldn't be using realloc. In fact in my opinion you shouldn't be using realloc even if the answer to both those questions is yes - you should be using a std::vector or other container instead.

Enigma
Quote:Original post by Enigma
Was the memory allocated by malloc, calloc or realloc? Are the smart pointers POD types? If the answer to either of these questions is no then you shouldn't be using realloc. In fact in my opinion you shouldn't be using realloc even if the answer to both those questions is yes - you should be using a std::vector or other container instead.

Enigma


It was allocated by malloc. Can you tell me what you mean by POD type?

Thanks
Clicky.

Enigma
I think I am narrowing down my problem a little here. My biggest issue at the moment seems to be the destruction of the dynamic array class.

At the moment the class destructor just does...
		~DynamicArray()		{			// now free array			free(m_array);		}

This means if it is holding a list of smart pointers, that the reference count is not getting released. I can't just go through and do...
			// first delete items			for (int i=0; i < m_count; i++)				m_array=NULL;

because because there is no guarantee that the class is holding a list of pointers.

How have others overcome this problem?

Thanks
Make a templated, type safe, dynamic array, and use new and delete for resizing it.

Or, use one of the standard library dynamic containers; std::vector if you want continious memory.
Quote:Original post by c2_0
Make a templated, type safe, dynamic array, and use new and delete for resizing it.

Or, use one of the standard library dynamic containers; std::vector if you want continious memory.


Cool. I changed my mallocs and frees to 'new' and 'delete []'. It seems to work great now. For the array resize, I had to use...
				// create new array				m_arraySize+=m_growBy;				T* newArray=new T[m_arraySize];				// copy members				for (int i=0; i < m_count; i++)					newArray=m_array;				// delete old array and replace				delete [] m_array;				m_array=newArray;

which seems to work, but may not be the most efficient?

Thanks!
template<typename T>class DynamicArray{	protected:		int	m_size;		T*	m_pBuffer;	public:		DynamicArray() : m_pBuffer(NULL), m_size(0)		{		}		DynamicArray(int size) : m_pBuffer(NULL), m_size(0)		{			Create(size);		}		~DynamicArray()		{			if (m_pBuffer)				delete [] m_pBuffer;		}		void Create(int size)		{			assert(!m_pBuffer && "Array already created");			m_size = size;			m_pBuffer = new T[size];		}		void Resize(int size)		{			int copySize = min(size, m_size);			T* pDest = new T[size];			T* pSrc = m_pBuffer;			T* pOriginal = m_pBuffer;			m_pBuffer = pDest;			m_size = size;			while (copySize--)				*(pDest++) = *(pSrc++);							delete [] pOriginal;		}		int GetSize()		{			return m_size;		}		operator T*()		{			return m_pBuffer;		}		T* DataPtr()		{			return m_pBuffer;		}		const T& operator [](int index) const		{			assert((index >= 0) && (index < m_size) && "Index out of range!");			return m_pBuffer[index];		}		T& operator [](int index)		{			assert((index >= 0) && (index < m_size) && "Index out of range!");			return m_pBuffer[index];		}};


[Edited by - e-u-l-o-g-y on September 6, 2005 5:18:43 PM]
my-eulogy - A blog about coding and gfxsdgi - Semi-Daily Game IdeaChunkyHacker - Viewer for Relic chunky formats (used in DOW)
Quote:Original post by e-u-l-o-g-y
*** Source Snippet Removed ***


Am I missing something? How is that dynamic? How does the array re-size itself?
oh.. hehe... I just remembered that I removed that from the class a few months ago :P ...never mind the post then ;)
my-eulogy - A blog about coding and gfxsdgi - Semi-Daily Game IdeaChunkyHacker - Viewer for Relic chunky formats (used in DOW)

This topic is closed to new replies.

Advertisement