1

What happen if deallocate object allocated with malloc using delete?
I'm trying making ObjectPool System.
So I wrote this codes.

struct A
{
    int a;
    int b;
    int c;

    A()
    {
        std::cout << "A constructor" << std::endl;
    }
    ~A()
    {
        std::cout << "A destructor" << std::endl;
    }
};

A* a = (A*)std::malloc(sizeof(A) * 10);
    
A* a0 = a;
new (a0) A();
delete a0; 

A* a1 = a + 1;
new (a1) A();
delete a1;  

.
.
.

A* a9 = a + 9;
new (a9) A();
delete a9;  

Pooling Class allocate multiple objects.
And Pooling Class don't mind about deallocating.

But I worried Deallocating memory(allocated with malloc) using delete may make memory leak. Does this codes make memory leak?

I will never call std::free(a).
Just let user deallocate memory themself.

SungJinKang
  • 409
  • 3
  • 9

1 Answers1

2

From: https://en.cppreference.com/w/cpp/memory/new/operator_delete:

If the pointer passed to the standard library deallocation function was not obtained from the corresponding standard library allocation function, the behavior is undefined.

Also, for the first definition:

void operator delete  ( void* ptr ) throw();        (until C++11)
void operator delete  ( void* ptr ) noexcept;       (since C++11)

it says:

The behavior of the standard library implementation of this function is undefined unless ptr is a null pointer or is a pointer previously obtained from the standard library implementation of operator new(size_t) or operator new(size_t, std::nothrow_t).

So, this means if you use new, you must use delete on that memory, and if you use malloc(), you must use free() on that memory, or else the behavior is undefined.

"undefined" doesn't mean the program doesn't work. It means the language definition doesn't specify what shall happen, so it may or may not work, depending on your compiler's implementation. But, you must NOT count on it to work, so it is considered a bug, and should be fixed.

Note about the reference information

https://en.cppreference.com/w/ is a wiki anyone can edit. It is not the official standard, but it is the closest open-source thing we have to it. Another public standards reference source is http://www.cplusplus.com, which is frequently not as up-to-date, but when it has explanations, I have found them to be much better in that they are easier to understand, but sometimes worse in that they are incomplete or don't cover the latest versions of C++ after C++11. So, use both together. If you're looking for a more-pedantic and more-up-to-date explanation, rely more on cppreference.com, and if you're looking for a more beginner-friendly and easier-to-understand explanation, rely more on cplusplus.com inasmuch as its information is complete and up-to-date.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • 1
    Definitely can't mix-and-match, that's a short trip to **undefined behavior** (and on my machine, a crasher because `new` and `malloc` have separate heaps). It is okay to `new`, and never `delete`. Or to `malloc`, and never `free`. – Eljay Jun 05 '21 at 18:32
  • 1
    it is usually ok these days to not free if your intent is to keep the memory until end of process, but on some older systems, or certain levels (kernel), it is not ok to malloc and never free except in scenarios where you'd keep the memory until a clean reboot. With c++ destructors though, its usually a trivial implementation anyway! – Abel Jun 05 '21 at 22:57
  • Um... Then If i want to allocate memory in advance and call constructor later, How can i do this?? And Deallocating is called by delete. Then As you said, I can't use malloc. But If i use new, It call constructor. How can i solve this problem?? – SungJinKang Jun 06 '21 at 06:36