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?