lundi 1 février 2016

new and delete on different scope

Consider the following code:

int *expand_array(int *oldarr,int array_length)
{
    int *newarr = new int[array_length +3];
    for(int counter=0;counter<array_length)
    newarr[counter]=oldarr[counter];

    delete[] oldarr;
    return newarr;
}

int main()
{
     int *myfirstarr = new int[4];
     int *my_expanded_arr=expand_array(myfirstarr,3);
     delete[] expanded_arr;
 }

will there be any memory leak here? And to generalise the question, if the pointer return from a new statement is copied passed to a function will the delete copied_pointer release the memory?

Aucun commentaire:

Enregistrer un commentaire