Array Dynamic resize in heap I have answered a Question in Stackoverflow link.


  a) Create a function called resize that can be used to increase the
  size of integer arrays dynamically. The function takes three
  parameters. The first parameter is the original array, the second
  parameter is the size of this array, and the third parameter is the
  size of the larger array to be created by this function. Make sure
  that you allocate memory from the heap inside this function. After
  allocating memory for the second array the function must copy the
  elements from the first array into the larger array. Finally, the
  function must return a pointer to the new array.
  
  b. In main, allocate an array on the heap that is just large enough to
  store the integers 5, 7, 3, and 1.
  
  c. Resize the array to store 10 integers by calling the resize
  function created in step a. Remove the old (smaller) array from the
  heap. Add the numbers 4, 2, and 8 to the end of the new array.
  
  d. Write a sort function that sorts any integer array in increasing
  order.
  
  e. Use the sort function to sort the array of numbers in c above.
  Display the sorted numbers.


Is there a Dangling pointer issue.

#include &lt;array&gt;
#include &lt;iostream&gt;

void swap(int *xp, int *yp) 
{ 
    int temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
} 

//Bubble Sort
bool sort(int arr[], int size)
{
    for( int i = 0; i&lt; size -1; i++)
    {
        for( int j = 0; j &lt; size - i -1; j++)
        {
            //descending order
            if(arr[j]&lt;arr[j+1])
            {
                swap(&amp;arr[j], &amp;arr[j+1]);
            }
        }
    }
    return true;
}

void Print(int Array[], int nSize)
{
    for( int i = 0; i &lt; nSize; i++)
    {
        std::cout&lt;&lt;" "&lt;&lt;Array[i];
    }
    std::cout&lt;&lt;"\n";
}

void Resize( int *&amp;Array, const int&amp; nSizeOld, const int&amp; nSize )
{
    int * newArray = new int[nSize];

    //Copy Elements of the Array
    for(int i = 0; i&lt; nSize; i++)
    {
        newArray[i] = Array[i];
    }

    delete[] Array;

    //Assign ptr of Prev to new Array
    Array =  newArray;
}

int _tmain(int argc, _TCHAR* argv[])
{
    const int kNewSize = 10, kSize = 5;
    int *pMyArray = new int[kSize];

    //Set Values
    for( int i = 0; i&lt; kSize; ++i ) 
    {
        pMyArray[i] = i * 5;
    }

    Resize( pMyArray, kSize, kNewSize );

    //Set Values
    for( int i = kSize; i&lt; kNewSize; ++i ) 
    {
        pMyArray[i] = i * 10;
    }

    Print(pMyArray, kNewSize);

    sort(pMyArray, kNewSize);

    Print(pMyArray, kNewSize);

    if( pMyArray!=NULL )
    {
        delete[] pMyArray;
    }

    return 0;
}
If you had tagged this code as C, it would have been acceptable. Since you tagged it as C++, it's horrible.

Instead of writing your own swap function, there's already std::swap in &lt;algorithm&gt;.

Instead of writing bubble sort yourself, just use std::sort, also from &lt;algorithm&gt;.

Instead of using arrays and resizing them yourself, just use std::vector&lt;int&gt;, from &lt;vector&gt;.

After applying these transformations, you cannot have a dangling pointer anymore since your code is completely pointer-free.

As part of an exercise for learning the basic operations on memory management, it's ok to write code like this, but never ever use such code in production. In production the code should look like this:

#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

void Print(const std::vector&lt;int&gt; &amp;nums)
{
    for(int num : nums)
    {
        std::cout &lt;&lt; " " &lt;&lt; num;
    }
    std::cout &lt;&lt; "\n";
}

int main()
{
    std::vector&lt;int&gt; nums { 5, 7, 3, 1 };

    // There's probably a more elegant way to add the elements to the vector.
    nums.push_back(4);
    nums.push_back(2);
    nums.push_back(8);

    std::sort(nums.begin(), nums.end());

    Print(nums);
}


By the way, your original code doesn't have any dangling pointer as well. Well done.

You don't need the != NULL check before the delete[] since that pointer cannot be null. In modern C++ (since C++11 I think) you would also write nullptr instead of NULL. The reason is that historically NULL had not been guaranteed to be of pointer type.

Have a look at https://en.cppreference.com/w/cpp/algorithm for more algorithms that you shouldn't implement yourself in C++.

I would have liked to write the push_back block in a shorter way, as well as the Print function. I'm sure there's a more elegant way, I just don't know it.