Search code examples
c++pointersmemorydynamicallocation

How can I free any dynamic memory allocated inside a function?


I'm working on a problem and I've been asked to create a function that returns a pointer. The program I wrote works fine and all; however, I create dynamic memory allocation inside my function called *newArray and I don't think I'm freeing that allocated memory the pointer ptr2 is pointing to inside this function. Is there a way to release this memory?

I was thinking of adding another parameter to the function - the ptr2 pointer from the main function - and just alter that pointer inside the function, but that defeats the purpose of the function returning a pointer. Any thoughts here?

Here's my code:

//Problem #12
#include <iostream>
using namespace std;

void SizeEntry(int*);
void DataEntry(int*, int);
void ShowArray(int *, int);
int *newArray(int *, int);

int main()
{
    int size, new_size;
    int *ptr=nullptr, *ptr2 = nullptr;

    //grab size and dynamically allocate an array to the entered size
    SizeEntry(&size);
    ptr = new int[size];

    //make size of second array
    new_size=size+1;

    //Fill array with data and make a new copy w/ requirements
    DataEntry(ptr, size);
    ptr2 = newArray(ptr, size);

    cout<<"\nHere is the first array: \n";
    ShowArray(ptr, size);
    cout<<"Here is the new array: \n";
    ShowArray(ptr2, new_size);

    //free allocated array
    delete [] ptr;
    delete [] ptr2;
    ptr = nullptr;
    ptr2 = nullptr;

    return 0;
}
void SizeEntry(int *size)
{
    cout<<"Enter the size of your list: ";
    cin>>*size;
    while(*size<0)
    {
        cout<<"Size must be non-negative, enter another number: ";
        cin>>*size;
    }
}
void DataEntry(int *ptr, int size)
{
    for(int count = 0; count<size; count++)
    {
        cout<<"Enter data for entry #"<<(count+1)<<": ";
        cin>>*(ptr+count);
        while(*(ptr+count)<0)
        {
            cout<<"Data point cannot be negative, try again: ";
            cin>>*(ptr + count);
        }
    }
}
void ShowArray(int *ptr, int size)
{
    for(int count=0; count<size; count++)
        cout<<*(ptr+count)<<" ";
    cout<<endl;
}
int *newArray(int *ptr, int size)
{
    int *ptr2=nullptr;
    int new_size = size+1;
    int counter=0;

    ptr2 = new int[new_size];

    for(int count=0; count<new_size; count++)
    {
        if(count==0)
            ptr2[count]=0;
        else
        {
            ptr2[count]=ptr[counter];
            counter++;
        }
    }        
    return ptr2;
}

Solution

  • To Free the memory allocated by "new Operator" in C++, Just use delete operator and nothing else.

    No need to assign null to the pointer whose memory is already freed by delete operator.

    PS: IF YOU INTENDED TO USE THAT POINTER AGAIN, ONLY THEN ASSIGN IT TO NULL.

    So in your case the last two line of code are useless unless you are intended to use that pointer again.

     //free allocated array
        delete [] ptr; 
        delete [] ptr2;
        ptr = nullptr; //Useless as memory is already free assigned to ptr 
        ptr2 = nullptr;//Useless as memory is already free assigned to ptr2
    

    In the *newArray function you are returning one pointer i.e ptr2.

    When you return ptr2 a copy of the value (an address) of ptr2 is made and returned to the caller. ptr2 was declared with automatic storage duration. It is the memory it refers to that must be freed and you are doing it in main as ptr2 of *newArray is assigned to ptr2 of main function.

    PS :If you have any doubts debug it in Visual Studio you will get the clear Idea.