简体   繁体   中英

initialize an empty unordered_map inside my structure in C++

I have a std::unordered_map<char, Node*> inside my custom structure. I want to initialize it with an empty map . Did I do something wrong? I have tried 2 kinds of initialization, both of them give me same result.

The following statement:

newStructure->map = unordered_map<char, Node*>();

sometimes results in success and the size of this map is 0, which it should be. Most of the time, this would fail during the initialization, and the following error would be generated with no initialization of the map :

malloc: pointer being freed was not allocated

This would give me extreme huge size of the initial std::unordered_map . The size of the map could be 88029716824088 .

How can I correctly initialize an empty std::unordered_map struct?

My structure is defined like this:

struct Node {
    char letter;
    unordered_map<char, Node*> next;
    bool hasEnd;
    static Node* init(char);
}

And, my initializer is defined like this:

Node* Node::init(char letter) {
    Node *newNode = (Node*) malloc(sizeof(Node));
    newNode->letter = letter;
    newNode->hasEnd = false;
    return newNode;
};

Sometimes, the size of the next would be very huge number.

You need to use new instead of malloc when creating C++ objects.
When you create a map it is already empty by default.

You can read more here: In what cases do I use malloc and/or new?

The actual failure and the strange map size happens because when you do this:

Node *newNode = (Node*) malloc(sizeof(Node));

You are saying that there struct Node at the address you got from malloc .

The problem is that the constructor for the unordered_map will not get called. And malloc is not required to return zeroed memory.

This means that internals of unordered_map will simply be whatever values are there in the memory returned by malloc . Also keep in mind that zeroes also might not be ok for correct initialization of unordered_map internals. It will only be correctly initialized if its constructor gets called. This you a Node with a broken unordered_map member

Then when you do:

newStructure->map = unordered_map<char, Node*>();

The destructor for the unordered_map will get called, because you are actually replacing the unordered_map which you said is already there with a new one. Since the values in the memory you got from malloc might not be 0 and the unordered_map internals might contain a pointer to some memory that gets allocated when its constructor gets called, the destructor will try to deallocate some random value, causing the error you are seeing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM