Function that returns pointer gives crash when trying to return pointer to main program

Asked

Viewed 161 times

1

I have this function that retrieves a record from a binary file already filled with records - such records are structured as Node - and returns it to the program. Only in the return of "i" for the main program variable "test", both being structured type Node, the program ceases to respond.

I’m willing to change any function parameter, but I need her to read one Node of the binary file and return this Node for the main program.

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

typedef struct node 
{
    void ** pointers;
    int * keys;
    struct node * parent;
    bool is_leaf;
    int num_keys;
    struct node * next; 
} node; 

    node* carrega_node (char *nome){   
        FILE * fp;  
        node * i;  
        fp = fopen (nome, "rb");  
        fread (&i, sizeof(node), 1, fp);  
        fclose (fp);  
        return i;  
    };  

    int main()  
    node * teste  

    teste = carrega_node ("candidatos.idx");
    return 0;
  • How is the struct node? The layout it fits perfectly in what comes from the file? In the file you probably have a text. You can not just play this text to the struct. You can, but it’s probably not what you want to do. Plus you haven’t allocated memory for i.

  • As I suspected you have much more serious problems to upload the file information to memory. I can’t imagine what the file looks like. You can put a snippet of it?

  • Added Node structure, but note that not much is relevant to us. You say that in the file I probably have text, which is not true. Think of Node as any structured type. The file here is a binary file that stores nodes. The goal is to retrieve a Node and return it to the main program.

  • You may not have a text but there is something that will not fit in the structure. As I do not know what you have, can not help.

1 answer

3


There has been some confusion with the syntax, it does not do what it seems to do. Note:

node* i; // Cria uma variável local do tipo node*. Esse ponteiro aponta para nada

fread(&i, sizeof(node), 1, fp); // Escreve sizeof(node) na variável i. Observe que
                                // i é um ponteiro que tem apenas 4 ou 8 bytes. Mas
                                // sizeof(node) pode ser bem maior que isso. Você
                                // está tentando ler um nodo ou um ponteiro para um nodo?

Here are two problems. The first is that it reads an entire node of the file and writes over the pointer itself to a node. And the second problem is that if you want to read a node, you first need to allocate enough memory to store that node. Correction:

node* i = malloc(sizeof(node)); // Aloca memória que caiba um nodo.

fread(i, sizeof(node), 1, fp);  // Lê um nodo e coloca nessa memória.
//    ^ note que estou passando o ponteiro para a memória alocada,
//      não um ponteiro para o ponteiro.

Remember that after you have finished using the node, you must release the memory that was allocated using free(i).


But why the crash occurred? Assume a pointer has 4 bytes and a node has 16 bytes.

sizeof(node)   // 16
sizeof(node*)  // 4

At the moment fread(&i, sizeof(node), 1, fp); is executed, the program will write 16 bytes of data into a variable that fits 4 bytes. The extra 12 bytes will be blindly written in memory corrupting any data that was there before. Curiously the information that was there before was the return address of the function. Soon, at the time that the return, execution will skip to another unrelated part of the program, out of main. And this will cause a crash.


But you have an even more serious problem:

You are saving and reading your file structure. But this structure contains pointers. There is no problem at all in saving pointers in a file. But they have no useful meaning. Because when you load them, in another run of the program, they will be pointing to a memory that has nothing. The memory layout of a process changes completely when it re-enters. And saving the pointer to data is not similar to saving the data itself.

It is not directly possible to save a lynched list to a file. First you need to turn it into a linear array or something that doesn’t depend on pointer behavior to work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.