this might be a simple question so accept my appolegises , I am creating a 2 dimetional pointers to int and I want to release the memory at the end but I think I'm doing It all wrong and all of the memory isn't being relaesed.

int **a;
a = new int *[bin->height];
for (int i=0;i<bin->height;i++)
a[i] = new int [bin->width];
for (int i=0;i<bin->height;i++)
            delete [] a[i];
            delete a;

am I doing it right ?

Dani AI

Generated

Short answer for : the pattern of allocating many separate row buffers is fragile (easy to leak, and not exception-safe). As and pointed out, the immediate problem is deallocation mismatch and manual cleanup. For real code, prefer RAII—either a standard container or a tiny wrapper—so you never have to pair new/delete yourself.

A simple, safe and fast approach is one contiguous block with index math. It uses a single allocation, is cache-friendly, and the memory is released automatically when the container goes out of scope:

size_t height = /*...*/, width = /*...*/;
std::vector<int> data(height * width);

auto at = [&](size_t r, size_t c) -> int& { return data[r * width + c]; };

at(0,0) = 1;    // use as data[r*width + c]

If convenience trumps contiguity, std::vector<std::vector<int>> grid(height, std::vector<int>(width)); gives grid[r][c] semantics and automatic cleanup, but each row is a separate allocation (so slightly worse cache locality).

Notes and troubleshooting:

  • Allocating rows in a loop is not exception-safe: if one new throws, previously allocated rows leak unless you free them in a catch. That’s another reason to use std::vector.
  • Use AddressSanitizer (compile with -fsanitize=address on GCC/Clang) or tools like Valgrind (Linux) / VS CRT debug heap to find leaks.
  • If you must keep the pointer-of-pointers design for API reasons, wrap it in a class with a destructor that frees everything—this centralizes cleanup (as suggested) and prevents mistakes across your codebase.

Switching to std::vector or a small RAII class will make the code simpler, safer, and far less likely to leak.

Very close, and conceptually you've got it perfect. The only problem is a mismatch of new[] and delete when releasing a. This is what you want:

int **a;

a = new int *[bin->height];

for (int i=0;i<bin->height;i++)
    a[i] = new int [bin->width];

for (int i=0;i<bin->height;i++)
            delete [] a[i];

delete [] a;

When using new, you should have a corresponding delete. When using new[], you should have a corresponding delete[].

This is when having a class to handle this cruft is called for. That way, you can deal with the new/delete cruft in the constructors/distructor and when you are done with the instance, just delete the class instance. IE,

void 2darrayfunct(size_t height, size_t width)
{
    2darray array = 2darray(height, width);
    // Do stuff with array here.
    .
    .
    .
    // Now, done with array.
}

Leaving the function, the array is automatically destroyed as it has gone out of scope. Simple, complete, no leaks (assuming the 2darray class destructor is properly implemented).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.