mardi 18 juillet 2017

C++11 way of dealing with HeapAlloc/malloc

I'm trying to find a smarter way for memory allocation within my program. I am running into "Not Enough Quota" errors on my HeapAllocs and I would like to try smart pointers to see if I get the same error.

I see that you can do:

`#include <memory>

struct MyDeleter
{
  void operator()(double *p) { free(p); }
};

int main()
{
  auto Data =
    std::unique_ptr<double, MyDeleter>{
        reinterpret_cast<double*>(malloc(sizeof(double) * 50)) };
  return 0;
}`

Is HeapAlloc the same way? With the changes from free to HeapFree and so forth? Is this even worth trying?

`#include <memory>

 struct MyDeleter
 {
   void operator()(HANDLE h, PBYTE p) { HeapFree(h, 0, p); }
 };

 int main()
 {
    auto Data =
    std::unique_ptr<BYTE, MyDeleter>{
        reinterpret_cast<BYTE*>(HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwSize)) };
    return 0;
 }

Aucun commentaire:

Enregistrer un commentaire