My code is leaking. How can I solve it?

Jatson

My code is leaking. I should delete somewhere the array, what I allocated in this line: T* out_array = new T[size1+size2]; But I don't know where and how. Can anyone help me please?

The code:

#include <iostream>
using namespace std;

template <class T>
T* merge(T arr1[], int size1, T arr2[], int size2);

template <class T>
T* merge_sort(T arr[], int n)
{
    if(n < 2){return arr;}
    int mid = n/2;
    T *arr1 = merge_sort<T>(arr,mid);
    T *arr2 = merge_sort<T>(arr+mid,n-mid);
    return merge(arr1, mid, arr2, n-mid);
}

template <class T>
T* merge(T arr1[], int size1, T arr2[], int size2)
{
    int i = 0,j = 0;

    T* out_array = new T[size1+size2];

    while((i < size1) && (j < size2))
    {
        if(arr1[i] >= arr2[j])
        {
            out_array[i+j] = arr2[j];
            ++j;
        }
        else
        {
            out_array[i+j] = arr1[i];
            ++i;
        }
    }
    while(i < size1)
    {
        //copy the reminder
        out_array[i+j] = arr1[i];
        i++;
    }
    while( j < size2)
    {
        out_array[i+j] = arr2[j];
        j++;
    }

    return out_array;
}

int main()
{
    int a[] = {2, 42, 3, 7, 1};
    int *a2 = merge_sort(a,5);
    for (int i = 0; i<= 4 ; ++i) cout << a2[i] << endl;
    delete[] a2;
    return (0);
}
Matthieu M.

I remember intervening "in urgence" on an application that was leaking badly:

  • the improvements needed to be loaded quickly
  • the application was already leaking, though less badly
  • the non-regressions were known to be incomplete
  • the application had never run under valgrind (it's not trivial to run multi-threaded code with tight timeout dependencies under valgrind...)

So, what did I do ? I used grep and removed all calls to new from the code (*).

  • in C++03: delete is an error (**) and new is a code smell
  • in C++11: both delete and new are an error (**)

And not so surprisingly, all memory leaks disappeared!

Instead of using new, you can use:

  • std::vector for a dynamically allocated array
  • std::unique_ptr for a dynamically allocated object
  • std::shared_ptr in some rare and arcane situations where the actual lifetime of an object obeys complex rules

    (*) or I could have if it had been C++11, in C++03 and in the absence of perfect-forwarding and variadic templates having a make_auto_ptr was not really possible.

    (**) in C++03 it could be argued that an expert writing boost::scoped_ptr (or equivalent) might need it; in C++11 you can build about every abstraction on top of unique_ptr because it's 0-cost.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i solve my bootstrap grid?

From Dev

How can I reliably prevent my local IP address leaking in the web browsers?

From Dev

How can i solve duplication of code in this?

From Dev

How can I solve the errors in this DirectX code?

From Dev

How can i solve my code to check size of data type (is c language with gcc compiler) on linux mint

From Dev

how can i solve this error in my win32 reading image file code

From Dev

How can I solve this visual glitch in my JavaFX TableView?

From Java

How can I solve my connection error in mysql

From Dev

How can i made my app run and solve this error

From Dev

How can I organize my Java code?

From Dev

How can i adapt my code to Asynktask

From Dev

How can i optimize my Jquery code?

From Dev

How can I correct my Java code ?

From Dev

How can I protect my robot code?

From Dev

How can i update the midpoint in my code?

From Dev

How can I add exponent in my code

From Dev

JAVA,how can I loop my code

From Dev

how can i speed up my code?

From Dev

How can I optimize my code for my Spanish Translation Program?

From Dev

How can I share code between my application and my service?

From Dev

How can I put this php code(if condition) in my html code?

From Dev

My image upload option is not working in my codeigniter application.How can i get solve this?

From Dev

How can I solve this with metaprogramming?

From Dev

How can I solve MongoWaitQueueFullException?

From Dev

How can I mprove my code so that it can be more standard?

From Dev

How can I mprove my code so that it can be more standard?

From Dev

How do I stop leaking implementation details in my Azure Mobile app's OData errors?

From Dev

How to solve SonarQube complaints about my code?

From Dev

How to solve SonarQube complaints about my code?

Related Related

  1. 1

    How can i solve my bootstrap grid?

  2. 2

    How can I reliably prevent my local IP address leaking in the web browsers?

  3. 3

    How can i solve duplication of code in this?

  4. 4

    How can I solve the errors in this DirectX code?

  5. 5

    How can i solve my code to check size of data type (is c language with gcc compiler) on linux mint

  6. 6

    how can i solve this error in my win32 reading image file code

  7. 7

    How can I solve this visual glitch in my JavaFX TableView?

  8. 8

    How can I solve my connection error in mysql

  9. 9

    How can i made my app run and solve this error

  10. 10

    How can I organize my Java code?

  11. 11

    How can i adapt my code to Asynktask

  12. 12

    How can i optimize my Jquery code?

  13. 13

    How can I correct my Java code ?

  14. 14

    How can I protect my robot code?

  15. 15

    How can i update the midpoint in my code?

  16. 16

    How can I add exponent in my code

  17. 17

    JAVA,how can I loop my code

  18. 18

    how can i speed up my code?

  19. 19

    How can I optimize my code for my Spanish Translation Program?

  20. 20

    How can I share code between my application and my service?

  21. 21

    How can I put this php code(if condition) in my html code?

  22. 22

    My image upload option is not working in my codeigniter application.How can i get solve this?

  23. 23

    How can I solve this with metaprogramming?

  24. 24

    How can I solve MongoWaitQueueFullException?

  25. 25

    How can I mprove my code so that it can be more standard?

  26. 26

    How can I mprove my code so that it can be more standard?

  27. 27

    How do I stop leaking implementation details in my Azure Mobile app's OData errors?

  28. 28

    How to solve SonarQube complaints about my code?

  29. 29

    How to solve SonarQube complaints about my code?

HotTag

Archive