-1
#include<iostream>

#include<stdio.h>

#include<stdlib.h>

#include<stack>

using namespace std;


typedef struct BTNode

{

  int n, *K, *A;

  struct BTNode **P;

} BTNode;

BTNode *getBTNode(int m){

    BTNode* node = (BTNode*)malloc(sizeof(BTNode));

    node->n = 0;

    node->K = (int *)malloc(sizeof(int) * (m-1));

    node->A = nullptr;

    node->P = (BTNode**)calloc(m, sizeof(BTNode*));


    return node;

}


typedef struct BTNode *BTree;


int binarySearch(int K[], int n, int key){

    int i = 0;

    while(i < n & key > K[i]){

            if(key == K[i]){return i;}

        i = i+1;

    }


    return i;


}

void insertBT(BTree *T, int m, int newKey){

    BTree* x = T;

    BTree* y = nullptr;

    stack<BTree*> st;

    stack<int>ist;

    int i;

    while((*x)!= nullptr){

        i = binarySearch((*x)->K,(*x)->n,newKey);

        if(i<(*x)->n && newKey == (*x)->K[i]){

            return ;

        }

        st.push(x);

        ist.push(i);

   
     (*x) = (*x) -> P[i];
    
}
    while(!st.empty()){
        
      if(!st.empty()){
            st.pop();
       
 }

        if(!ist.empty()){

            ist.pop();

        }



        if((*x)->n < m-1){

            (*x)->K[i+1] = newKey;

            (*x)->n = ((*x)->n) + 1;
 
           if((*y) != nullptr){

                (*x)->P[i+1] = (*y);

            }
            return ;
        }

        BTNode* tempNode = getBTNode(m+1);
        tempNode = (*x);
        tempNode->K[i+1] = newKey;
        tempNode->P[i+1] = (*y);

        *y = getBTNode(m);
        if(m==3){
            (*x) -> K[0] = tempNode ->K[0];
            (*x) -> P[0] = tempNode ->P[0];
            (*x) -> P[1] = tempNode ->P[1];
            (*y) -> K[0] = tempNode ->K[1];
            (*y) -> P[0] = tempNode ->P[2];
            (*y) -> P[1] = tempNode ->P[3];
        }
        newKey = tempNode->K[m/2];

        delete tempNode;
    }

    (*T) = getBTNode(m);
    (*T) -> K[0] = newKey;
    (*T) -> P[0] = (*x);
    (*T) -> P[1] = (*y);
    (*T) -> n = 1;


}

void inorderBT(BTNode* T){

    if(T !=nullptr){

        cout<<T->K[0];



    }
}

int main()

{

    FILE *f;

    for(int m=3; m<=3; m++){

        BTree T = nullptr;

        f = fopen("./insertSequence.txt", "r");

        for(int n; !feof(f);){

            fscanf(f, "%d", &n);

            insertBT(&T, m, n);

            printf("/n");

        }

        fclose(f);



        /*f = fopen("./deleteSequence.txt", "r");

        for(int n; !feof(f);){

            fscanf(f, "%d", &n);

            insertBT(&T, m, n);

            inorderBT(T);

            printf("/n");

        }

        fclose(f);*/

    }

}

B Tree is being implemented.

(*T) = getBTNode(m);

(*T) -> K[0] = newKey;

(*T) -> P[0] = (*x);

(*T) -> P[1] = (*y);

(*T) -> n = 1;

An error occurs at this part. 0xC0000005 What's the problem?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • You're accessing an uninitialized pointer. – πάντα ῥεῖ Nov 26 '21 at 13:58
  • 1
    Are all these (blank) lines really necessary? Is that really the [mcve] necessary to reproduce the problem? – πάντα ῥεῖ Nov 26 '21 at 14:01
  • When you feel the need to do a C-style cast (like you do with your `malloc` call) you should take that as a sign you're doing doing something wrong. In your case it's using `malloc` to allocate C++ objects. In "modern" C++ you should be using *smart pointers* like [`std::shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr) of [`std::unique_ptr`](https://en.cppreference.com/w/cpp/memory/unique_ptr). Or if for some reason you need raw non-owning pointer use `new` to allocate objects. – Some programmer dude Nov 26 '21 at 14:02
  • This is 95% c. One c++ thing you are doing is `delete`ing a node you allocated with malloc. Dont do that - `free` matches with `malloc`, `delete` matches with `new`. – Mike Vine Nov 26 '21 at 14:02
  • And to make the code more readable and maintainable, don't use one-letter variable names. Use names that fits the context or purpose of the variables. And use comments when things might not be as clear. There are other problems as well, like using [`feof` in a loop condition](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong), or using C stdio file to begin with. Using `typedef` for the structure, which is not needed. And creating a type-alias of a pointer, which make the code harder to read and understand. And pointers instead of references in some arguments. – Some programmer dude Nov 26 '21 at 14:03

1 Answers1

0

Did you consider using new and delete?
It'd probably give better errors, and you won't get a nullptr from calling new. (There is an exception)
Other than that: I cannot answer the question unless you provide valid code.
Undefined behavior imposes no restriction on the behavior of the program.

BTree* x = T;
while((*x)!= nullptr) { //...

What do you think just happened, here? *x dereferences the nullptr, != nullptr is always true (or false. Can be anything, really. It's UB, sky's the limit).

viraltaco_
  • 814
  • 5
  • 14