Jump to content
  • Advertisement
Sign in to follow this  
Raeldor

Ouch! Exception During Realloc of Dynamic Array of Smart Pointers

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!