replacing malloc/free with new/delete in c++

c++, memory-management

Solution

Technically, it is correct. However, this is C++ we are talking about, and the C++ way to dynamically allocate an array is to use a `std:vector` instead:

std::vector<int> Image(m_Width/2 * m_Height);

Or:

std::vector<int> Image;
Image.resize(m_Width/2 * m_Height);

The memory will be freed automatically when the `std::vector` is destructed when it goes out of scope.

Problem

I just want to be sure. this is my code ``` int * Image = (int *)malloc(sizeof(int) * m_Width/2 * m_Height); free(Image); ``` if I want to use new Instead of malloc and free instead of delete. this is what i wrote ``` int* Image = new int[m_Width/2 * m_Height]; delete[] Image; ``` Is that correct?

Original source

Related problems