Home > Blockchain >  Program stops when I call function and displays an Exception thrown at 0x00007FFC0DCC2487 (ucrtbased
Program stops when I call function and displays an Exception thrown at 0x00007FFC0DCC2487 (ucrtbased

Time:12-11

I have an array of structs. Here is my struct:

struct tree {
    itemType name[MAX];
    struct tree* left;
    struct tree* right;
};

typedef struct tree* treenode;

and I am creating this function to print out the names:

void printRoot(treenode root[]){

    int i = 0;
    while (root[i] != NULL) {
        printf("\n\t\t%c %s", 254, root[i]->name);
        i  ;
    }
}

When I call my printRoot function, my program stops and gives me an error of Exception thrown at 0x00007FFC0DCC2487 (ucrtbased.dll) 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF. and then opens up the file of stdio.h.

I tried not using a loop to see if my program really works:

        char folname[MAX];
        printf("Enter folder name: ");
        scanf(" %s", &folname);

        ROOT[0] = createFolder(folname);
        printf("%s", ROOT[0]->name);

and it does. So my assumption is that the problem is really within my printRoot function, but I can't seem to find out what it is that's causing it to not run.

--- [EDIT] --- This is actually sort of a project in school in which we are to create some sort of a file explorer. So my idea is to create an array with structs in it to make a BST in each index.

we are tasked to create 3 different files: main, implementation, and header. So here is my code for more info (this is unifinished):

#include "Header.h"

int main() {

    treenode ROOT[50];
    char choice;

    printf("\n\t\tDo you want to create a folder? [Y/N]: ");
    scanf("%c", &choice);

    choice = tolower(choice);

    if (choice == 'y') {
        itemType folname[MAX];
        printf("\n\t\tEnter folder name: ");
        scanf(" %s", folname);

        ROOT[0] = createFolder(folname);
        printf("%s", ROOT[0]->name); //this works
        //printRoot(ROOT); --> this is the problem i'm trying to fix


    }
    else
    {
        printf("\ntest");
    }

implementation:

treenode createFolder(itemType x[]) {
    treenode touch = (struct tree*)malloc(sizeof(struct tree));
    strcpy(touch->name, x);
    touch->left = NULL;
    touch->right = NULL;

    return touch;
}

void printRoot(treenode root[]){

    int i = 0;

    while (root[i] != 0) {
        printf("\n\t\t%c %s", 254, root[i]->name);
        i  ;
    }

}


CodePudding user response:

It looks like you are trying to loop through the array of treenode pointers, but you are using a null check on each pointer instead of on the array itself. This means that when you reach the end of the array and try to access a pointer that has not been initialized, your program crashes with an access violation error.

To fix this, you can use a null check on the array itself to determine when you have reached the end of the array. You can do this by checking if the array element at the current index is null, and if it is, you can break out of the loop. Here is an example of how you could do this:

void printRoot(treenode root[]){
int i = 0;
while (1) {
    // Check if the current array element is null
    if (root[i] == NULL) {
        // If it is, break out of the loop
        break;
    }
    printf("\n\t\t%c %s", 254, root[i]->name);
    i  ;
}
}

Alternatively, you could use a for loop instead of a while loop, and use the array size as the loop limit. This would also prevent the access violation error from occurring. Here is an example of how you could do this:

void printRoot(treenode root[], int size) {
    for (int i = 0; i < size; i  ) {
        // Check if the current array element is null
        if (root[i] == NULL) {
        // If it is, break out of the loop
            break;
        }
        printf("\n\t\t%c %s", 254, root[i]->name);
    }
}

You would then need to pass the size of the array to the printRoot() function when you call it, like this:

printRoot(ROOT, 50);

Hope this helps! Let me know if you have any other questions.

  • Related