Search code examples
c++new-operatorfreesecurezeromemory

How to release BYTE memory?


Quick question: do I have to / how do I release memory from this allocation of memory?

unsigned char *uString = new unsigned char[4096]();

And if I have to, would I do it like this?

free(uString); delete[] uString;


Solution

  • You use the free function when you allocate memory with the C functions like malloc calloc and more.

    In c++ you use the operators new and delete (Also thier array friends new[] and delete[]).

    When you want to delete object created with the operator new[], you call the operator delete[]. For example:

    int main(void)
    {
        int[] arr = new int[5];
        int* a = new int;
        delete[] arr;
        delete a;
        return 0;
    }
    

    And that's all. That will also call the destructor an all of that.