CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 25

Threaded View

  1. #1
    Join Date
    May 2013
    Posts
    5

    Heap Corruption during run time.

    Hello, I am having a problem with my code. I am trying to delete an array, after I created a bigger one, and copied all the values from the previous to the new one, but when i try to delete the old one, I get : heap corruption detected after normal block. Please help me. Here is the code:

    this is the add function:

    Code:
    bool IntArrayList::add(Number& num)
    {
    	if(m_Arr[0] == NULL)///If array is empty put the first element at 0 index
    	{
    		m_Arr[0] = #
    		m_Size += 1;
    		return true;
    	}
    	else
    		{
    			int in_place = SearchInsertPlace();///find the place where you store another element
    			if(in_place != -1)///there is still place available
    			{
    				m_Arr[in_place] = #
    				m_Size += 1;
    				return true;
    			}
    			else///if no free places, create new array
    			{
    				m_Arr = CreateNewArray(m_Arr);
    
    				in_place = SearchInsertPlace();
    				m_Arr[in_place] = #
    				return true;
    			}
    		}
    }

    this is the function that creates the new array:

    Code:
    Number** IntArrayList::CreateNewArray(Number** arr)
    {
    	m_ArraySize *= 2;
    	Number** newArray = new Number*[m_ArraySize];
    		for(int i=0 ; i<m_ArraySize ; i++)
    			newArray[i]=NULL;
    
    	for(int i = 0 ; i < m_Size ; i++ )/// creating new array.
    		{
    			newArray[i] = m_Arr[i];
    			m_Arr[i]=NULL;
    	    }
    	//for(int i = 0 ; i < m_Size ; i++)/// delete old array.
    	//	delete m_Arr[i];
    	delete []m_Arr;
    
    	return newArray;
    }
    thanks!
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured