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