Heap allocation consists of a few steps:
- pointer allocation on a stack
-
sizeof(T)
bytes allocation on a heap -
T
’s constructor call on allocated memory - the memory address assignment to the pointer
-
manual deallocation using
delete
operator
void heap()
{
int *p = new int(100);
delete p;
}
void heap()
{
int *p;
p = (int*)malloc(sizeof(int));
*p = 100;
free(p);
}