Im trying to implement a btree. I don't have any error in compiling, but when I run my code I have some weird runtime error. It says something like: MALLOC c:3096: sYsmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned......
why is this happening? I already tested that the problem is when I call crear_nodo(), which creates a new nodo, and returns me the pointer of the new node
btree* D=crear_nodo(aux, temp); //runtime error!
please help. I need to find the problem :'(

Bellow is the structure of the tree:

typedef struct nodo{
    int clave[3];
    struct nodo* rama[4];
    struct nodo* padre;
    int color;
    int peso;   //controls the number of keys in the node
}nodo;
typedef struct nodo* btree;

This is my code for the insertion method:

void dividir_nodo(btree* hoja, int dato, int pos){
    btree* x;
    int temp;     //keeps the fouth key
    int mediana;  //saves the medium key which is going up, by default is clave[2] of the repositioned keys
    if (pos==1){  //key is in clave[0]
        temp=(*hoja)->clave[2];
        (*hoja)->clave[2]=(*hoja)->clave[1];
        (*hoja)->clave[1]=(*hoja)->clave[0];
        (*hoja)->clave[0]=dato;
        mediana=(*hoja)->clave[2];
    }
//int pos tells me the position of the new key in the node
    //--------------------------------//
     btree* D=crear_nodo(aux, temp); //creation of the right node, which contains the clave[2] of *hoja
    //--------------------------------//
    (*hoja)->clave[2]=NULL;  //modifies node hoja to eliminate the key that is now in node D

    if ((*hoja)->padre==NULL){  //if *hoja is the tree root, creates a new node for the mediana, which is going up
        btree* P=crear_nodo(aux, mediana)
        //setting up the pointers:
        (*hoja)->padre=*P;
        (*D)->padre=*P;
        (*P)->rama[0]=*hoja;
        (*P)->rama[1]=*D;
    }
    else //if hoja->padre already exists, just moves up the mediana
         subir_mediana(hoja, D, &(*hoja)->padre, mediana);       
}

this is the method which creates a new node:

btree* crear_nodo(btree* r, int dato){
    *r= (nodo*)malloc(sizeof(btree));
    (*r)->clave[0]=dato;
    (*r)->clave[1]=NULL;
    (*r)->clave[2]=NULL;
    (*r)->rama[0]=NULL;
    (*r)->rama[1]=NULL;
    (*r)->rama[2]=NULL;
    (*r)->peso=1;  
    (*r)->color=0;
    return r;
}

aux and hoja are global variables.
NOTE: I used aux just for testing, I get the same error if I do: btree* D=crear_nodo(D, temp);
Google says that this happends because Im trying to allocate something of size 0, but I already check temp and mediana, they are not cero. Otherwise, if I just do: btree D=new (nodo);
I get the same error. Help please. T__T

This is incorrect because you typedef btree as a pointer already.

btree* D=crear_nodo(aux, temp);

try this instead.

btree D=crear_nodo(aux, temp);

I already changed that line for: btree D=new (nodo); and btree D= (nodo*)malloc(sizeof(btree));
but Im getting the same error. I think this is caused by malloc, or something else. I already changed the compiler and the PC, but the same error is displayed.
I can't see what is wrong with my code. HELP PLEASE

I think this is caused by malloc, or something else.

It's caused by something else. That something else is you misusing malloc. Tell me, what is sizeof(btree) and what makes you think it's equal to sizeof(nodo)? Allocating less memory than you use is a good way to corrupt the memory manager.

Tell me, what is sizeof(btree) and what makes you think it's equal to sizeof(nodo)?

I really don't understand. btree is a pointer o nodo, ok, but why is wrong if I do this: nodo D= new(nodo); ?

When doing malloc(sizeof(btree)); , you are allocating enough memory to store a pointer to a node (that is was btree is, just a pointer to a node, not a node). That does not, in any way, allocate any memory to store the node content. When you are later accessing the node that this new pointer is pointing to (which is nowhere, btw), you are just corrupting your memory, big time. One annoying thing with memory corruption problems is that the error messages you get are typically unrelated to the error itself. When you corrupt memory, it can corrupt almost any kind of memory, including the heap itself, other memory in the heap, stack memory, executable code, and the list goes on. And you are not even always guaranteed that it will crash, but usually it does, and when it crashes, it is not the corrupting (or faulty) code that crashes but the code which is affected by the corruption, which often has very little to do with the faulty code. So, conclusion, as Narue said, "It's caused by something else.".

But, of course, your code in the crear_nodo() function is a blatant memory corruption, so, I would start by fixing that, and then revisiting all your code for similar memory corruption problems. Here is a more reasonable version of it:

btree* crear_nodo(btree* r, int dato){
    *r= (nodo*) malloc( sizeof(nodo) ); //notice sizeof(nodo)
    (*r)->clave[0]=dato;
    (*r)->clave[1]=NULL;
    (*r)->clave[2]=NULL;
    (*r)->rama[0]=NULL;
    (*r)->rama[1]=NULL;
    (*r)->rama[2]=NULL;
    (*r)->peso=1;  
    (*r)->color=0;
    return r;
}

Another important aspect is, you have mentioned that you interchanged malloc and new to see if that couldn't magically fix the code. First of all, it can't. Second, and most importantly, you have to realize that malloc and new are NOT interchangeable, because they have to match with their corresponding deallocation function (malloc memory is freed with free(), and new memory is freed with delete). Having mismatched allocation and deallocation functions is likely to corrupt the heap, which can be even worse than memory corruption problems.

thanks for all your responses. I was forgetting to change crear_nodo().
no errors now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.