Why is the (virtual) destructor of the derived class not called when deleting an array through a pointer to the base class?

Note that whilst a Cat is an Animal, an array of Cats is not an array of Animals. In other words, arrays are invariant in C++, not covariant like they are in some other languages. So you are up-casting this array and this later confuses the compiler. You must do array delete[] in this case …

Read more

Why use _mm_malloc? (as opposed to _aligned_malloc, alligned_alloc, or posix_memalign)

Intel compilers support POSIX (Linux) and non-POSIX (Windows) operating systems, hence cannot rely upon either the POSIX or the Windows function. Thus, a compiler-specific but OS-agnostic solution was chosen. C11 is a great solution but Microsoft doesn’t even support C99 yet, so who knows if they will ever support C11. Update: Unlike the C11/POSIX/Windows allocation …

Read more

C++ polymorphism without pointers

Ultimately, no. Polymorphism only works with non-value types: references and pointers. And since references can only be bound once, you cannot really use them in standard containers. That leaves you with pointers. You’re attacking the problem at the wrong end. If you are concerned about the overhead of allocating lots of small objects (and I’m …

Read more

Why is there a memory leak in this program and how can I solve it, given the constraints (using malloc and free for objects containing std::string)? [duplicate]

The important pieces of your code line by line… Allocate memory for one Person object: auto p = (Person*)malloc(sizeof(Person)); Construct a Person object in that already allocated memory via calling its constructor: p = new(p)Person(); Free the memory allocated via malloc: free(p); Calling the constructor via placement new creates a std::string. That string would be …

Read more