I am trying to create a tree from a vector of parents. However I get a "corrupted heap error" when I create the nodes with malloc. It works for the first two children , however crashes on the third(or terminates but does not connect the root with the child.)
Unhandled exception at 0x77E8A879 (ntdll.dll) in lab7.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77EC5910).
I have implemented this by, first extracting the root and creating the node and after that extracting the children of the root and creating them.
search root-> searches for the root and returns the value of it. the function that searches for the children(search key )
void create->The function that creates the children. I send a vector that contains only the children of that specific parent and not other children.
*typedef struct node
{
int value;
node *left;
node *right;
node *middle;
}TreeNodeParent;
int search_root(int in[9])
{
for (int i = 0; i < 9; i++)
{
if (in[i] == -1)
{
int var = i+1;
in[i] = -2;
return var;
}// pe else nu facem nimic
}
return -2;
}
int search_key(int in[9], int radacina)
{
for (int i = 0; i < 9; i++)
{
if (in[i] == radacina)
{
int var = i + 1;
in[i] = -2;
return var;
}
}
return -3;
}
TreeNodeParent *createOneNode(int value)
{
//the error appears here
TreeNodeParent* create = (TreeNodeParent*)malloc(sizeof(TreeNodeParent));
create->value = value;
create->left = create->middle = create->right = NULL;
return create;
}
void create(int vector[], TreeNodeParent* radacina)
{
for (int i = 0; i < 9; i++)
{
if (vector[i] == -3)//am stabilit ca -3 ii oprirea
{
break;
}
else
{
TreeNodeParent* create = createOneNode(vector[i]);
if (radacina->left == NULL)
{
radacina->left = create;
}
else if (radacina->middle == NULL)
{
radacina->middle = create;
}
else
{
radacina->right = create;
}
}
}
}
int main()
{
int input[9] = { 2,7,5,2,7,7,-1,5,2 };
int root = search_root(input);
if (root == -2)
{
printf("Nu gasim radacina, arbore incorect!");
}
else { printf("root %d", root); }
//crearea nodului parinte
TreeNodeParent* rootParent = (TreeNodeParent*)malloc(sizeof(TreeNodeParent*));
rootParent->value = root;
rootParent->left = NULL;
rootParent->right = NULL;
rootParent->middle = NULL;
int vect2[9];
for (int i = 0; i < 9; i++)//worst case, tot arborele is copii ai lui root->o(n2)
{
vect2[i] = search_key(input, root);
printf("copii rootului %d", vect2[i]);
if ( vect2[i] == -3)
{
break;
}
}
create(vect2,rootParent);
_getch();
return 0;
}
checked online with gdb :
> tin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((ol > d_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed. program exited > with code 134
I can't understand why the function crashes only on the third(and not before). Also it does not always appear. Sometimes it works fine, other times it stops with that error. Also If there is a better way to create a tree with a parent representation?
I think the problem is in your main()
method, where you malloc()
with sizeof(TreeNodeParent*)
instead of sizeof(TreeNodeParent)
and assign it to rootParent
:
//crearea nodului parinte
//TreeNodeParent* rootParent = (TreeNodeParent*)malloc(sizeof(TreeNodeParent*));
TreeNodeParent* rootParent = (TreeNodeParent*)malloc(sizeof(TreeNodeParent)); `
User contributions licensed under CC BY-SA 3.0