Home > other >  Read access violation when trying to compare int?
Read access violation when trying to compare int?

Time:11-01

I am getting a read access violation and I cant understand why. I though I allocated everything correctly and I shouldn't be reading past the end of anything? Any ideas or help is greatly appreciated.

I am trying to read in a file that has a single integer per line and store those into a binary tree and linked list. The tree actually holds the data and the linked list just holds pointers to the nodes in the tree.

The error happens in the insert() function when data and node->num are being compared.

The newNode() function creates both a tree node and a linked list node, it only returns the tree node because of the double pointer handling the linked list.

// Garrett Manley

// delete these when turning it in
#pragma warning(disable : 4996)

#include <stdio.h>
#include <stdlib.h>

// define structs
struct treeNode {
    int num;
    struct treeNode *right, *left;
};

struct listNode {
    struct treeNode *tNode; // data is a pointer to the tree
    struct listNode *next;
};

// creates and returns a new node with given data, also adds node to linked list 
struct treeNode *newNode(int data, struct listNode *(*list)) {
    // make tree node
    struct treeNode *node = malloc(sizeof(struct treeNode));
    node->num = data;
    node->left = NULL;
    node->right = NULL;
    // make list node
    // insert entry into linked list
    struct listNode *newNode;
    newNode = malloc(sizeof(struct listNode));
    newNode->tNode = node;
    newNode->next = *list;
    *list = newNode;

    return (node);
}

// inserts given node into the tree in sorted order
struct treeNode *insert(struct treeNode *node, int data, struct listNode *(*list)) {
    if (node == NULL) { // if the tree is empty return new node
        return (newNode(data, list));
    } else { // if there is a node use recursion to get to the bottom of the tree and add a node in the right spot
        if (data <= node->num) {
            node->left = insert(node->left, data, list);
        } else {
            node->right = insert(node->right, data, list);
        }
        return (node); // return the (unchanged) node pointer
    }
}

// print linked list by looping through list, keep looping until node.next == null
// while looping print like this node.data.data to get the tree nodes data
void printList(struct listNode *(*list)) {
    struct listNode *tmp = *list;
    while (tmp->next != NULL) {
        tmp = tmp->next;
        printf("here");
    }
}

// skim through the file and find how many entries there are
int SCAN(FILE (*stream)) {
    int size = 0;
    char *str = malloc(100 * sizeof(char));
    while (fgets(str, 100, stream) != NULL) {
        size  ;
    }
    return size;
}

// loop through the file and load the entries into the main data array
void LOAD(FILE *stream, int size, struct treeNode *(*tree), struct listNode *(*list)) {

    rewind(stream);
    int i;
    char *tmp = malloc(100 * sizeof(char));
    for (i = 0; i < size; i  ) {
        fgets(tmp, 100, stream);

        // recursively call insert to create the list, fix
        *tree = insert(*tree, atol(tmp), list);
    }
}

// free up everything
void FREE(struct treeNode *BlackBox, int size) {
    // free linked list first
    // then free the tree nodes, do this recursively
}

int main(int argv, char *argc[]) {

    FILE *file = fopen("./hw7.data", "r");

    int size = SCAN(file);

    struct treeNode *tree = malloc(size * sizeof(struct treeNode));
    struct listNode *list = malloc(size * sizeof(struct listNode));

    LOAD(file, size, &tree, &list);
    
    // print output

    // print linked list
    //printList(list);

    fclose(file);
    
    return 0;
}

CodePudding user response:

There is no need to allocate memory in main for the tree, nodes are allocated by insert as needed. Allocating uninitialized memory for tree and list causes undefined behavior when the members pointed to by these pointers are dereferenced.

Change the initializations to:

    struct treeNode *tree = NULL;
    struct listNode *list = NULL;

Also note that the parentheses in return (node); are unnecessary, it is customary to write return node; instead.

CodePudding user response:

Was allocating memory twice, removed malloc() in main() and now program works.

  • Related