This is for a school project, so no need to ask details as to why I'm using an array of lists (I'm supposed to make a custom HashMap without using unordered map). My question is, when creating this array in the constructor (the array also needs to be allocated using dynamic memory), what happens in the case it couldn't allocate memory and "new" fails? Since each list is allocated on the heap, does that mean that it could fail midway, and there could be memory leakage?
Example:
class A
{
private:
std::list<std::string> * a;
public:
A() : a(nullptr)
{
a = new std::list<std::string>[10];
}
};
So for example, could this fail midway while creating the lists, which will cause memory to leak? If so, how would I use nothrow in this case? How do I know where it stopped and failed? Should I initialize each list independently?