0

I am trying to create a binary search tree and i'm tackling a problem where when i'm trying to add a value in a struct parameter, it immediately gives an exception write access violation. i have two structs , one is a pointer to a BST which contain a Student Node pointer, and a Student Node struct which contain some variables(ID,grades,...), left, right and a parent pointer this are the structs:

typedef struct StudentNode {
    int ID;    //node key
    int MidtermGrade;
    int ExamGrade;
    struct StudentNode* left;
    struct StudentNode* right;
    struct StudentNode* parent;
} StudentNode;

typedef struct BST {
    struct StudentNode* root;
} BST;

this are the functions that i made to create a BST and a new student node

BST* CreateBST() {
    int id;
    BST* tree = (BST*)malloc(sizeof(struct BST));
    StudentNode* root = (StudentNode*)malloc(sizeof(struct StudentNode));
    printf("Enter Id of the first student\n");
    scanf("%d", &id);
    root = new_node(id);
    tree = root;
    return tree;
}

struct StudentNode* new_node(int idNum) {
    struct StudentNode* Student = (StudentNode*)malloc(sizeof(struct StudentNode));
    Student->ID = idNum;
    Student->ExamGrade = 0;
    Student->MidtermGrade = 0;
    Student->parent = NULL;
    Student->left = NULL;
    Student->right = NULL;
    return Student;
}

and when i'm getting to the first line that need to change a variable in the newly built struct(Student)

Student->ID = idNum;

it throws the exception

Exception thrown: write access violation.
**Student** was 0xFFFFFFFF9A2D56F0.
JHBonarius
  • 10,824
  • 3
  • 22
  • 41
MaxShap
  • 39
  • 6
  • You are not checking if `malloc` fails. In C++, use `new` instead. But `CreateBST` has 2 memory leaks, and wouldn’t even compile in C++. Get rid of the 2nd `malloc` and change `tree = root` to `tree->root = root` – Remy Lebeau Dec 12 '20 at 17:55
  • @RemyLebeau i added now check for the malloc fails, and fixed the `tree->root = root` part, the malloc doesn't fail for now , but still isn't helps with the main issue... – MaxShap Dec 12 '20 at 17:58
  • Also, how do you *know* where the crash happens? Have you caught it in a debugger on that specific line? What is the value of `Student` when the crash happens? – Some programmer dude Dec 12 '20 at 17:59
  • @Someprogrammerdude yes i use Visual studio debugger, just getting from the start until that line. one line before the crash the Student has value: `0x000000003bfe56f0 {ID=??? MidtermGrade=??? ExamGrade=??? ...} – MaxShap Dec 12 '20 at 18:02
  • Then you [shouldn't really be casting of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Dec 12 '20 at 18:02
  • 2
    Also that code alone won't be causing a *read* access violation. It could cause a *write* access violation if you forgot to include the `` header file, or if `malloc` returned a null pointer. Please try to create a [mcve] and [edit] your question to show it. Also please tell us the values of all involved variables at the time of the crash. – Some programmer dude Dec 12 '20 at 18:04
  • @Someprogrammerdude thank you very much! everything works great , look like i just needed the stdlib library and a few tweaks in the syntax – MaxShap Dec 12 '20 at 20:54

0 Answers0