I know that we normally don't call operator new
& operator delete
function directly. Because If I do so constructor & destructor won't be called & it likely ends up with memory leak. But for primitive types this seems not problematic. Correct me If I am wrong.
Consider following program:
#include <iostream>
int main()
{
int* p=(int*)operator new (3);
std::cout<<*p;
operator delete(p);
}
Why it prints garbage value as output? What I have done wrong here? I am curious to know about how to correctly give value by calling operator new function directly. I want the output of program 3 as a output, but by not using new operator like int* p=new int(3);. What should I do?