Initializing objects inside of constructors C++

I am new to C++ and cannot figure out this error I am getting. I have created a Card class that stores a suit and rank as an unsigned integer. I also have a Maingame class that runs multiple functions. I want to create 5 Card objects inside of my Maingame class, but I am… Read More Initializing objects inside of constructors C++

I am getting an error with my leet code solution and i don't understand it

The problem in question is Two Sum. This is the error message: ================================================================= ==31==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000001a0 at pc 0x000000346130 bp 0x7ffcd0ff6df0 sp 0x7ffcd0ff6de8 READ of size 4 at 0x6020000001a0 thread T0 #2 0x7f5dcdb820b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) 0x6020000001a0 is located 0 bytes to the right of 16-byte region [0x602000000190,0x6020000001a0) allocated by thread T0 here: #6… Read More I am getting an error with my leet code solution and i don't understand it

Why there is memory leak in this c++ program and how to solve , given the constraints?

This is Minimal Working Example, for the problem I am facingin in my real code. #include <iostream> namespace Test1 { static const std::string MSG1="Something really big message"; } struct Person{ std::string name; }; int main() { auto p = (Person*)malloc(sizeof(Person)); p = new(p)Person(); p->name=Test1::MSG1; std::cout << "name: "<< p->name << std::endl; free(p); std::cout << "done"… Read More Why there is memory leak in this c++ program and how to solve , given the constraints?

Why at the moment of sorting the object vector the counter of created objects of the class is increased by an additional 2?

There is a HasPtr class: class HasPtr { friend void swap(HasPtr&, HasPtr&); public: HasPtr(const std::string& s = std::string()) : ps(new std::string(s)), i(0), use(new std::size_t(1)) {} HasPtr(const HasPtr& hp) : ps(hp.ps), i(hp.i), use(hp.use) { ++*use; } HasPtr& operator=(HasPtr hp) { swap(*this, hp); return *this; } ~HasPtr() { if (–*use == 0) { delete ps; delete use;… Read More Why at the moment of sorting the object vector the counter of created objects of the class is increased by an additional 2?