2

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.

Azeem
  • 7,659
  • 4
  • 22
  • 36
Judgelight
  • 71
  • 1
  • 8
  • if u define a map it is already empty. – user1438832 Oct 16 '17 at 03:48
  • @user1438832 but the size of the map could sometimes go very large. How could that possible? – Judgelight Oct 16 '17 at 03:50
  • 4
    You are not using `malloc` to allocate C++ objects, are you? [In what cases should I use malloc vs new?](https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new) – Bo Persson Oct 16 '17 at 04:24
  • @BoPersson, thank you so much. I just come from C, and I'm not very familiar with the detail of the C++. The map work normal now. I think I need to find out the difference between `new` and `malloc` now. – Judgelight Oct 16 '17 at 04:32
  • 2
    @Judgelight - It's rather simple really. Don't use `malloc` and you'll be fine. If you wonder "but what if I need to use `malloc`?" then you'll have to wait a long time until you have a real need. And by then you'll know enough C++ to use it wisely. – StoryTeller - Unslander Monica Oct 16 '17 at 06:03

1 Answers1

1

In fact, when you create a map it is already empty by default.
On the other hand, you need to use new, not malloc when creating C++ objects. The basic difference is that new calls the constructor of the object but malloc does not.
You can read more here: In what cases do I use malloc vs new?

roman
  • 95
  • 1
  • 6