Why constructor is not called in malloc?
This question already has an answer here:
malloc
and free
are purely memory-management functions, they don't know anything about classes (and existed long before C++). They're low-level memory manipulation.
C++ added classes to C, and as part of that process, added new
and delete
to create and destroy instances of classes. That's different from low-level memory management.
malloc
is a C function that pre-dates constructors. free
is a C function that pre-dates destructors. They are both C functions that must operate in a language without constructors and destructors.
上一篇: malloc是否创建类的新实例?
下一篇: 为什么构造函数不在malloc中调用?