0

I have a problem with my allocation in this program, I used Valgrind and the error I receive is:

Address 0xe9481f041af07000 is not stack'd, malloc'd or (recently) free'd

And when I compile I receive:

make: *** [Makefile:7: run] Segmentation fault (core dumped)

I am receiving errors in this part of my code, any ideas of what the problem could be?

#include <stdio.h>
#include <stdlib.h>
typedef struct 
{
    int left, right;
    int nr;
}intervals;
void read_alloc(int *n, intervals **v)
{
    int i;
    FILE *f = fopen("date.in", "r");
    fscanf(f,"%d", n);
    *v = (intervals *) malloc((*n) * sizeof(intervals));
    for (i = 0; i < *n; i++)
    {
       fscanf(f,"%d %d", &(v[i]->left), &(v[i]->right));
       v[i]->nr = i;
    }
    fclose(f);
}
int main()
{
    int n;
    intervals *v;
    read_alloc(&n, &v);
    for (int i = 0; i < n; i++)
    {
        printf("%d %d\n", v[i].left, v[i].right);
    }
    free(v);

return 0;

}

The input:

6
80 85
3 7
50 70
83 84
1 5
25 50

The compilation command:

gcc p1.c -Wall -g -o compiler
Jorj2014
  • 45
  • 1
  • 6
  • 7
    You show way too little code. How do you call that function? What values do yo pass? Please edit your question to provide a [MCVE](https://stackoverflow.com/help/mcve). – Gerhardh May 09 '22 at 16:15
  • 1
    You should run your program in a debugger. It should show you where the segfault happens – Gerhardh May 09 '22 at 16:15
  • 5
    You do not check any of your function calls for success. That is mandatory for I/O functions. What do you expect to happen if the file does not exist? How do you know if you could read values successfully without checking any return value? – Gerhardh May 09 '22 at 16:16
  • 2
    also, please choose a different name for your function, [`read`](https://man7.org/linux/man-pages/man2/read.2.html) already exists. – yano May 09 '22 at 16:21
  • 2
    The error is a runtime error. It is neither the compiler nor make that throw this error, it's your code. – the busybee May 09 '22 at 16:25
  • 1
    `&(v)[i]->left` looks fishy to me. I assume you want "the address of `v[i]->left`"? Maybe the order of operations gives you exactly that, but `&(v[i]->left)` makes that much more clear IMO. – yano May 09 '22 at 16:25
  • 1
    @thebusybee — it looks like the 'compilation' command was '`make run`', which compiles and then runs the code. I agree, it is not a compilation error. And it would be simpler if the OP reported on how the program was run, rather than simply giving the error from `make` when the program is run via `make`. – Jonathan Leffler May 09 '22 at 16:49
  • I modified the code using your suggestions but I receive the same error. – Jorj2014 May 09 '22 at 16:55
  • 2
    can you add `intervals` definition, add missing include, etc – Ôrel May 09 '22 at 16:58

1 Answers1

2

you mix v and *v, you should not use v but *v

static void read_alloc(int *n, intervals **v)
{
    int i;
    FILE *f = fopen("date.in", "r");
    fscanf(f,"%d", n);
    *v = (intervals *) malloc((*n) * sizeof(intervals));
    for (i = 0; i < *n; i++)
    {
       fscanf(f,"%d %d", &((*v)[i].left), &((*v)[i].right));
       (*v)[i].nr = i;
    }
    fclose(f);
}

Using another variable can be easier, (and don't cast malloc)

static void read_alloc(int *n, intervals **_v)
{
    int i;
    intervals *v;
    FILE *f = fopen("date.in", "r");
    /* TODO check f */
    fscanf(f,"%d", n);
    v = malloc((*n) * sizeof(intervals));
    *_v = v;
    for (i = 0; i < *n; i++, v++) {
         fscanf(f,"%d %d", &v->left, &v->right);
         v->nr = i;
    }
    
    fclose(f);
}
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • 1
    For the 2nd example, even simpler is to put `*_v = v;` _above_ the loop. Then, do: `for (i = 0; i < *n; i++, v++) { fscanf(f,"%d %d", &v->left, &v->right); v->nr = i; }` – Craig Estey May 09 '22 at 17:12
  • Why shouldn't I cast malloc? Is it just not necessary in this case? – Jorj2014 May 09 '22 at 17:54
  • 1
    @Jorj2014 https://stackoverflow.com/questions/7545365/why-does-this-code-segfault-on-64-bit-architecture-but-work-fine-on-32-bit – Ôrel May 10 '22 at 07:29