0

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;

JeffR
  • 765
  • 2
  • 8
  • 23
  • 4
    `new` goes with `delete`, `new[]` goes with `delete[]`. `malloc` goes with `free`. But since you use C++, you never use `malloc` or `free` (or better, you are advised to not use them) – Raildex Jan 18 '21 at 17:02
  • See: https://en.cppreference.com/w/cpp/memory/new/operator_new – Martin Jan 18 '21 at 17:02
  • The first result on google suggests a question with a high rated answer and 4 references. https://stackoverflow.com/questions/679571/when-to-use-new-and-when-not-to-in-c – Jose Jan 18 '21 at 17:04
  • @Raildex Well, there is one use for `malloc` still, and that's implementing containers. Reimplementing `std::vector` would require `malloc`. But the chance that you will need to implement a container from scratch (school assignments rarely require useful implementation) are next to 0. – Yksisarvinen Jan 18 '21 at 17:10
  • @Yksisarvinen `malloc()` is not required for creating containers. `new[]` can be used for that. – Remy Lebeau Jan 18 '21 at 17:57
  • Sidenote: When you are given memory by a function that isn't from the `*alloc` family or `new` consult the documentation for that function on how to release the memory. If you don't know how to release something, find out. Using the wrong method might look like it works right up until the the worst possible moment. – user4581301 Jan 18 '21 at 18:15

1 Answers1

2

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.

  • So the destructor will also null the memory bytes? – JeffR Jan 18 '21 at 18:29
  • 2
    @JeffR No. `free`,`delete` and `delete[]` are only guaranteed to do the absolute minimum, free the memory up for reuse. It doesn't necessarily mark the storage in any way or return it to the underlying system. Many debuggers will mark freed memory with an easily-identifiable pattern to help you spot bugs and some implementations may attempt to increase security by zeroing memory, but none of this is required because it will slow down programs that do not need this special treatment. – user4581301 Jan 18 '21 at 19:45
  • 1
    Thank you, great explanation. Do you see any issue with using this `#define` to free and clear the memory? `#define SAFENEWFREE(x) do { RtlSecureZeroMemory(x, _tcslen(x)); delete[] x; } while (0)` – JeffR Jan 22 '21 at 18:56
  • @JeffR, I think that if you use C++, you should use only `free` and `delete`. Because they are the safest option to have. If someone wants to have other way to delete memory, he just overrides the `delete` operator. If you use C, then it depends on the code itself. – Avihai Shalom Jan 24 '21 at 10:05