I am writing a program on binary trees.
To initialize the tree, I made a separate procedure, but when I call it, a very strange error appears.
In the code, the procedure is called initTree. The essence of the error is that after it works, the text in the console cannot be displayed normally, writes an error (also to the console):
Untitled^ malloc.c:2403: sysmalloc: Assertation '(old_top == initial_top (av) && old_size == 0) || ((unsigned long (old_size) >= MINSIZE && prev_inus (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed Also, if I run the debugger, qtCreator tells me about the SIGABRT error.
I experimentally found out that if you add the output of any text to the beginning of this procedure, then this text itself is not displayed, but in the future the error does not manifest itself and the output works fine.
I tested it with both <cstdio> and <iostream> , the result was exactly the same.
Without this procedure, the error does not appear either.
Here is the program code:
#include <iostream> #include <cstdio> using namespace std; void preparationToCreateTree(int &n, int &posA); void initTree(int &n, int &posA, int tree[], int numTree[], int &posZ); int main() { int posA = 1, posZ; int n; preparationToCreateTree(n, posA); int *numTree = new int[posA + n + 3 + 1]; int *tree = new int[n + 1]; initTree(n, posA, tree, numTree, posZ); cout << "TESTED" << endl; //Без строки ниже тут выкидывает ошибку return 0; } void preparationToCreateTree(int &n, int &posA) { cin >> n; while(posA < n) posA *= 2; } void initTree(int &n, int &posA, int tree[], int numTree[] , int &posZ) { cout << "Ahh.. Touch me, senpai!!"; //Без этой строки появляется ошибка tree[0] = -1; for(int i = 1; i <= posA + n + 3 + 1 - 1; i++) tree[i] = 0; for(int i = 1; i <= n; i++) cin >> tree[i]; for(int i = 1; i <= n; i++) numTree[posA + i - 1] = i; if(n % 2 != 0) n++, tree[posA + n - 1] = 0; if((n / 2) % 2 != 0) n += 2, tree[posA + n - 1] = 0, tree[posA + n - 2] = 0; posZ = posA + n - 1; } I am writing on archlinux. I use gxx compilers