简体   繁体   中英

Memory management when working with pointers in C++

I have these scenarios and I want to know if I manage my memory correctly. I watch the memory consumption in the Task Manager when I start the executable and see how memory is not popped back to the initial amount, which leads me to suspect that I don't clear memory where is needed. So, in this first case I have a function that adds a new element to a dynamic array:

struct Color {
    int R;
    int G;
    int B;
}

int TotalColors;
Color* Rainbow;

void AddColor(Color NewColor) {
    // So, I create a new array of size TotalColors+1
    Color* NewRainbow = new Color[TotalColors+1];
    // Now I add the existing elements
    for (int i=0; i<TotalColors; i++) {
        NewRainbow[i] = Rainbow[i];
    }
    // And lastly, I add the new element
    NewRainbow[TotalColors] = NewColor;
    // Now, I assign the NewRainbow to Rainbow (I don't know if it's correct)
    Rainbow = NewRainbow;
}

So, in this case, do you think I miss something? This is working but I want to make sure the unused stuff is removed from memory. I also have a function to remove an element, which looks like this:

void RemoveColor(Color Removable) {
    // Again, I create a new array of size TotalColors-1
    Color* NewRainbow = new Color[TotalColors-1];
    // I scan the list and add only those elements which are not 'Removable'
    for (int i=0; i<TotalColors; i++) {
        // Let's suppose that Removable exists in the list
        if (Rainbow[i].R != Removable.R && Raibow[i].G != Removable.G && ... {
            NewRainbow [i] = Rainbow[i];
        }
    }
    // Again, the same operation as above
    NewRainbow[TotalColors] = NewColor;
    Rainbow = NewRainbow;
}

In this case, I don't know what happens with Rainbow[Removable], I mean, the element of the array that is removed. And the last case, is this, where I try to send the pointer of an element from the array to a function.

Color* GetColor(int Index) {
    Color* FoundColor;
    // Scan the array
    for (int i=0; i<TotalColors; i++) {
        if (i == Index) FoundColor = &Rainbow[i];
    }
    return FoundColor;
}

// And I use it like this
void ChangeColor(int Index) {
    Color* Changeable;
    Changeable = GetColor(Index);
    SetRGB(Changeable, 100, 100, 100);
}

// And this is what changes the value
void SetRGB(Color* OldRGB, int R, int G, int B) {
    (*oldRGB).R = R;
    (*oldRGB).G = G;
    (*oldRGB).B = B;
}

And this is it. So, this works but I am not sure if with so many pointers I didn't forget to delete something. For example, when I RemoveColor I don't see the memory changed (maybe some bytes don't make the difference) and I just want some professional eye to tell me if I missed something. Thanks!

In the first function AddColor() you are not deleting the previously allocated memory.

Rainbow = NewRainbow; // leaking the memory Rainbow was previously pointing to.

Change that last line to:

delete[] Rainbow;
Rainbow = NewRainbow;

Same thing with RemoveColor()

Any time you use the new operator it needs to have a corresponding delete . Also, if you are allocating an array with new[] as in your case, it must have a corresponding delete[] .

In order not to worry whether you've forgotten to delete a pointer, you shouldn't use plain pointers. Instead, use smart pointers such as

std::shared_ptr
std::unique_ptr
etc.

or, if you don't have C++11 yet, use

boost::shared_ptr
boost::scoped_ptr

More on smart pointers, see Wikipedia and the specific documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM