0

Whenever I insert a new element in my B+ tree, it seemingly rewrites each data point with the one to be inserted, before the insert function even starts. The only possible cause I've found is that my bPLus object doesn't seem to have access to the Professor struct, which is held in each node. As in, if I'm in the debugger, all the Professor values are empty. The keys (cle) on the other hand, do work and are accessible. Might have something to do with the pointers?

When i wasn't working with the keys, I would get an "error reading characters of string" if I called certain functions from main, while others would work. And those same functions would work if called from a working function inside the class. I have a feeling these issues are all related.

I've included everything that might be involved, I am at a loss.

struct Professeur {
    string ID;
    string nom;
    string dept;
    string salaire;
};

struct Noeud {
    Professeur *valeur[3];
    string cle[3];
    Noeud *enfant[4];
    Noeud *parent;
    Noeud *suivant;
    bool isLeaf;

    Noeud(){
        for (int i = 0; i < 3; i++) {
            valeur[i] = NULL;
            enfant[i] = NULL;
            cle[i] = "vide";
        }
        isLeaf = 0;
        parent = NULL;
        suivant = NULL;
        enfant[3] = NULL;
    }
};

class bPlus {
private:
    Noeud* racine;
public:
    bPlus() {
        racine = new Noeud;
        racine->isLeaf = 1;
    }

    void inserer(Professeur x) {
        if (findValue(x.nom)) {
            return;
        }
        if (racine->isLeaf) {
            //do the insertion
        }

        else {
            //find leaf, do insertion
        }
    }
int main() {
    Professeur profs[12];

    //gather data from text file

    for (int i = 0; i < 12; i++) {
        profs[i].ID = ID[i];
        profs[i].nom = nom[i];
        profs[i].dept = dept[i];
        profs[i].salaire = salaire[i];
    }

    bPlus arbre;
    for (int i = 0; i < 3; i++) {
        arbre.inserer(profs[i]);
    }

    return 0;
}
  • Can you elaborate on the second paragraph? For what code does it work and for what doesn’t? – Ignatius Apr 09 '19 at 02:11
  • I think you have edited out too much code, but my guess is that you have something like `valeur[i] = &x;` inside `inserer()`. Storing the address of parameters doesn't work as you might hope, as the pointed-to location will be most likely overwritten on the next call. There should be a dup question desribing the reason, but I cannot find it right now though... – Ken Y-N Apr 09 '19 at 02:11
  • 2
    I would have put more code in, but since the issue happened before even starting the functions themselves, I figured it would just clutter the page up. You were right though, I switched the function to hold the address directly and after a few tweaks it worked out. I assume that passing the object as parameter made use of it's own address? So when it passed a new object as a parameter it updated each record of said address inside the database. Which in this case was the entire database. Thanks for the help! – John Smith Apr 09 '19 at 02:16

0 Answers0