Segmentation failure when changing a Struct’s value in C

Asked

Viewed 326 times

1

An error is occurring Falha de segmentação when trying to access information from a struct. The code is as follows:

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

typedef struct {
    int val;
}Info;

void inserir(Info*);

void main(){
    Info* info;
    inserir(info);

    //O erro acontece na linha de baixo, como se o
    //que foi feito na função inserir não persistiu de fato
    //na estrutura
    printf("%d\n", info->val);

}

void inserir(Info* info){
    info = (Info *)malloc(sizeof(Info));
    info->val=10;
}
  • The correct 'and use int main(). The use of void main() unnecessarily limits your program to implementations that accept this form.

2 answers

3


void main(){
    Info* info;
    inserir(info);

    //O erro acontece na linha de baixo, como se o
    //que foi feito na função inserir não persistiu de fato
    //na estrutura
    printf("%d\n", info->val);
}

What happens here is that the info variable does not change value before and after the function inserir().

The initial value of the variable (garbage because it was not initialized) is the value that will be used in the printf.

As you suggest in your answer, you can solve by assigning a value before calling the function; or instead of passing the value you pass the address.

int main(void) {
    Info *info;
    inserir(&info);
    printf("%d\n", info->val);
    free(info);
}
void inserir(Info **x) {
    *x = malloc(sizeof **x); // falta validacao
    (*x)->val = 10;
}

Still another solution is to use the function return value

int main(void) {
    Info *info;
    info = criarinfo();
    printf("%d\n", info->val);
    free(info);
}
Info *criarinfo(void) {
    Info *x;
    x = malloc(sizeof *x); // falta validacao
    x->val = 10;
    return x;
}

In my opinion your solution is the best!
Note that in the two solutions above the malloc and the free are in different places. It becomes much easier to manage the memory when the function that makes the malloc 'and responsible for also doing the free.

  • I couldn’t see that I had to pass the pointer pointer, vlw. Now I know when to use pointer.

  • Good solution in code. Could say in the written solution that is why the type parameter (Tipo *variavel) is only able to write by reference in the alias to which the combustible points, and that when using malloc in the function, try to assign a new pointer value, and not only change its alias.

  • @Rafaelbluhm: without using the concept of reference I said more or less that. The concept of reference (and indeed) is not an integral part of C: it is a foreignness of C++.

  • I actually wanted to say that the value of the variable could be changed directly, since basically a pointer is a way of referring to objects in memory. I went to save words and ended up being misunderstood. Where I speak "reference" read "passing simulation by reference".

1

The solution to this problem was to change the location of the malloc

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

typedef struct {
    int val;
}Info;

void inserir(Info*);

void main(){
    //A mudança está aqui
    Info* info = (Info *)malloc(sizeof(Info));
    inserir(info);

    printf("%d\n", info->val);
    free(info);

}

void inserir(Info* info){
    info->val=10;
}

From what I understand was passing an empty structure pointer with no memory address to the function inserir. If I’m wrong please comment.

  • Do not forget to release the memory when it is no longer necessary. In this case, do free(info); after the printf.

  • well remembered, is that I made a summary of the problem and here I forgot the free, the original code is another.

Browser other questions tagged

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