2

I'm working with C++ in Visual Studio.
I have a problem when using this structure:

struct TreeNode
{
    string info;
    TreeNode* left, * right;
};
typedef struct TreeNode* ExpTree;

like in this function:

ExpTree createNode(string info)
{
    TreeNode* temp;
    temp = (TreeNode*)malloc(sizeof(TreeNode));
    if (temp == NULL)
    {
        cout << "Out of space!\n";
        return (temp);
    }
    temp->left = NULL;
    temp->right = NULL;
    temp->info = info;
    return temp;
};

When I try to run this in the main function:

ExpTree tree = NULL;
tree = createNode(expresie);
cout << tree->info;

it prints nothing and exits with this code: -1073741819.

After debugging I saw that the program stops on this line: temp->info = info;, saying <Error reading characters of string>.

I made a little research on this and I saw that this has to do more with a bad design of the code, and not with a certain problem with a single solution.

So what did I do wrong here?

  • 3
    Why do you use `malloc` here? `malloc` will only allocate memory, but not initialize any of the classes. Use `new` instead, or even better, standard containers and smart pointers. – Lukas-T Jan 07 '21 at 09:46
  • Hi @churill, that seems plausible enough to make an answer, doesn't it? – Yunnosch Jan 07 '21 at 09:52
  • @Yunnosch Probably, but at the same time I see that mistake so often. I believe there might be a good duplicate for it. – Lukas-T Jan 07 '21 at 09:56
  • 1
    Does this answer your question? [What is the difference between "new" and "malloc" and "calloc" in C++?](https://stackoverflow.com/questions/807939/what-is-the-difference-between-new-and-malloc-and-calloc-in-c) and [this](https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new) is also related. – Lukas-T Jan 07 '21 at 09:58
  • I do think it answers. But I suspect that anybody who used `malloc()` in the first place might ask "If that is the difference, then why do I need to use it in my code?". – Yunnosch Jan 07 '21 at 10:00
  • I just tried and it works! Thanks – Ciprian Cristian Jan 07 '21 at 10:01
  • Initially I tagged it with the both of them but the administration removed the C tag. – Ciprian Cristian Jan 07 '21 at 10:32
  • Not "administration", but you are still basically right (and I missed it). Yes, that probably means that I am a little alone with my "both" opinion. Wise, to not follow my proposal. But thanks for following my other one. – Yunnosch Jan 07 '21 at 10:56

2 Answers2

2

I think that the proposed duplicate technically does answer the question.
But here is some specifics on how that explains the problem here.

C "strings", which is not actually a thing there, are only memory containing char, usually terminated with '\0'. What ever is done with them, is done by functions which expect nothing but chars in memory.

C++ std::strings are different, they are objects, with methods, overloaded operators etc.
They do need initialisation or will fail in almost all use cases, like you have observed.

The init is done with new() not with malloc().
That is why you should use new() and why (as you have confirmed by now) it solves your problem.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
1

Here is the solution. Use following code:

TreeNode* temp = new TreeNode();
//temp = (TreeNode*)malloc(sizeof(TreeNode));

Now the explanation. malloc only allocates memory of the size specified. It does not initializes it. In your case when you use string in the structure it is a class which needs initialization and without proper initialization the behavior of assignment is undefined.

using new is reconnended because it makes sure that not only the structure but all its components inclusing string info; gets initialized properly.