How to replace one dynamic array with another without causing memory leak?

SemperCallide

This is a simple question and I'm kind of a beginner, but I would just like to confirm this.

Say I have a class object int * data, which is pointing to an array of 5 ints. Say I have a function that would replace data with 5 arguments, as illustrated below.

void replaceData(int a, int b, int c, int d, int e){
    int * temp = new int[5];

    temp[0] = a;
    temp[1] = b;
    temp[2] = c;
    temp[3] = d;
    temp[4] = e;

    data = temp;           
}

I am concerned that this would cause a memory leak, because if data already had 5 ints in there, they would still be floating around. I am tempted to change it in the following way to avoid this:

void replaceData(int a, int b, int c, int d, int e){
    int * temp = new int[5];

    temp[0] = a;
    temp[1] = b;
    temp[2] = c;
    temp[3] = d;
    temp[4] = e;

    delete data;     //would this prevent a memory leak? 
    data = temp;
    temp = 0;           
}

Thank you very much for your input!

Steve Howard

Almost.

The delete operator is for pointers to single objects. You need to do delete[] data for this to work. Zeroing the temp pointer at the end doesn't make a difference.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cupy fft causing memory leak?

From Dev

Volley slow and causing memory leak

From Dev

iOS - NJSONSerialization causing a memory leak??(according to Instruments)

From Dev

Android runOnUiThread causing memory leak

From Dev

Why this code is not causing memory leak?

From Dev

Replace char* in function without memory leak

From Dev

NSStream.getStreamsToHostWithName causing a memory leak

From Dev

Adding a new object to ArrayList is causing memory leak?

From Dev

Why is CIContext.createCGImage causing a memory leak?

From Dev

Netty causing memory leak in tomcat

From Dev

Pyramid debugtoolbar causing memory "leak"

From Dev

Replace array item with another one without mutating state

From Dev

What is causing this memory leak in iOS

From Dev

Bitmap causing memory leak on device

From Dev

Using a reference in recursion is causing a memory leak?

From Dev

Adding a fragment and calling .replace() causing a memory leak in the added fragment

From Dev

iOS - NJSONSerialization causing a memory leak??(according to Instruments)

From Dev

How to replace one array key value with another array index value?

From Dev

Android runOnUiThread causing memory leak

From Dev

Replace char* in function without memory leak

From Dev

Finalizers not finalized for ThreadPoolExecutor, causing a memory leak

From Dev

drawViewHierarchyInRect causing VM CG Image memory leak

From Dev

Adding a new object to ArrayList is causing memory leak?

From Dev

Netty causing memory leak in tomcat

From Dev

What is causing this memory leak in iOS

From Dev

How to use a class with a context argument in a static context without causing a memory leak?

From Dev

Why/How is my code causing a memory leak?

From Dev

How to Replace One Array index with another array in Php

From Dev

CALayer causing memory leak with ARC

Related Related

  1. 1

    Cupy fft causing memory leak?

  2. 2

    Volley slow and causing memory leak

  3. 3

    iOS - NJSONSerialization causing a memory leak??(according to Instruments)

  4. 4

    Android runOnUiThread causing memory leak

  5. 5

    Why this code is not causing memory leak?

  6. 6

    Replace char* in function without memory leak

  7. 7

    NSStream.getStreamsToHostWithName causing a memory leak

  8. 8

    Adding a new object to ArrayList is causing memory leak?

  9. 9

    Why is CIContext.createCGImage causing a memory leak?

  10. 10

    Netty causing memory leak in tomcat

  11. 11

    Pyramid debugtoolbar causing memory "leak"

  12. 12

    Replace array item with another one without mutating state

  13. 13

    What is causing this memory leak in iOS

  14. 14

    Bitmap causing memory leak on device

  15. 15

    Using a reference in recursion is causing a memory leak?

  16. 16

    Adding a fragment and calling .replace() causing a memory leak in the added fragment

  17. 17

    iOS - NJSONSerialization causing a memory leak??(according to Instruments)

  18. 18

    How to replace one array key value with another array index value?

  19. 19

    Android runOnUiThread causing memory leak

  20. 20

    Replace char* in function without memory leak

  21. 21

    Finalizers not finalized for ThreadPoolExecutor, causing a memory leak

  22. 22

    drawViewHierarchyInRect causing VM CG Image memory leak

  23. 23

    Adding a new object to ArrayList is causing memory leak?

  24. 24

    Netty causing memory leak in tomcat

  25. 25

    What is causing this memory leak in iOS

  26. 26

    How to use a class with a context argument in a static context without causing a memory leak?

  27. 27

    Why/How is my code causing a memory leak?

  28. 28

    How to Replace One Array index with another array in Php

  29. 29

    CALayer causing memory leak with ARC

HotTag

Archive