Heap Corruption Detected

Well i am trying to add some numbers to an array, if the array is full the program creates a new, bigger array, and when i try to erase the old one i'm getting Heap Corruption Error. Please help.

Here's is the Adding Function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool IntArrayList::add(Number& num)
{
	if(m_Arr[0] == NULL)
	{
		m_Arr[0] = #
		m_Size += 1;
		return true;
	}
	else
		{
			int in_place = SearchInsertPlace();
			if(in_place != -1)
			{
				m_Arr[in_place] = #
				m_Size += 1;
				return true;
			}
			else
			{
				Number** newArr = CreateNewArray(m_Arr);
				in_place = SearchInsertPlace();
				newArr[in_place] = #
				return true;
			}
		}
}


here is the function that creates new array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Number** IntArrayList::CreateNewArray(Number** arr)
{
	m_ArraySize *= 2;
	Number** newArray = new Number*[m_ArraySize];

	for(int i = 0 ; i < m_Size ; i++ )/// creating new array.
		newArray[i] = m_Arr[i];

	//for(int i = 0 ; i < m_Size ; i++)/// delete old array.// this part doesnt' work at all
	//	delete m_Arr[i];///
	///delete m_Arr;////

	return newArray;
}
Last edited on
I think that the problem is that the new allocated array was not initialized by NULL(s). So its elements starting from m_Size have undefined values.
Also in member function add you forgot to increase m_Size

1
2
3
4
5
6
7
			else
			{
				Number** newArr = CreateNewArray(m_Arr);
				in_place = SearchInsertPlace();
				newArr[in_place] = &num;
				return true;
			}


Also you did not delete the old array.
Last edited on
Topic archived. No new replies allowed.