Getting segmentation error

Go To


Working on some code for a machine problem; we've just started working with pointers, etc, and so I'm not quite sure where it's wrong. Running debugging is showing that it's the line: for(i=0; i<*y;i++) that is throwing the error, though I'm sure whatever's wrong is also wrong in the for(j=0;j<*x;j++) space. Any help would be greatly appreciated.

    int readImage(char* fileName,
                  int image[MAX_IMAGE_SIZE][MAX_IMAGE_SIZE][NUM_CHANNELS],
                  int* x,
                  int* y,
                  int* max)
        /* Variable declarations*/
        int i=0; 
        int j=0;
        int k=0;
        int num=0;
        char c;
        /* Opens file, skips first line*/
        FILE *input=fopen(fileName, "r");
            return -1;
        /*Saves the x and y components into a variable */
        /*Cycles through file, reading it into the array */
        for(i=0; i<*y;i++)
                for(k=0;k<NUM_CHANNELS; k++)
                    /*Takes input */
                    fscanf(input, "%d",&num);
                    /*Stores into the array in the form of array[x][y][color] */
        /*Closes input  */
        return 0;
2012-04-04 01:42
by rbonick
This isn't enough to go on. We'll need to see the rest of your code - jwodder 2012-04-04 01:47


Variables x, y and max are already pointers. So you don't need to use address-of & in the fscanf(). Also, you need to allocate memory for them if the caller didn't.

Just use:


And make sure memory is allocated for them by the caller. Otherwise, use malloc()

2012-04-04 01:52
by P.P.
So then in the for loop should I just use x - rbonick 2012-04-04 01:55
No. In the for loops you wanted to use the values. So you should use *x, *y and *max respectively. Difference: x is a pointer and *x is a value pointed by x - P.P. 2012-04-04 01:56


You got a wrong thing there: you shouldn't pass &x or &y to scanf. Just pass x and y.

The explanation is as follows:

x and y, as defined by the function's parameters, are pointers to a memory location which stores and int, i.e. on the memory space of x and y, there is a memory address that indicates where the actual int is stored.

Then, there is the address-of operator (&). This operator gives you the memory address of the argument.

scanf normally asks for a memory address where it will store the value read, so normally when you have an int variable (say int i, for example) you give scanf its memory address with & (&i). But in this case, you are not asking for the memory address of the int, instead you are asking for the memory address of the pointer. For example, if you typed the value "5", then that would get stored on y (which, again, is a pointer, not an int) and then, when you use *y, the program would try to read the contents of the memory on address 5 which is very likely to not be a valid address for your program (hence segmentation fault).

Wow, this is really complicated to explain without drawing you a picture as I speak, I hope you get the point.

2012-04-04 02:00
by José Ernesto Lara Rodríguez
I do understand it. And this part of the code now works, thanks to you as well as the other answer. Thank you very much - rbonick 2012-04-04 02:06
Awesome!! Glad to be of help - José Ernesto Lara Rodríguez 2012-04-04 02:14


As others have said, the arguments to scanf() need to be pointers. As you already have a set of pointers (x, y and max) you don't need to dereference them when passing them to scanf()

So, instead of using:


you should use:


You said that the error was on the line: for(i=0; i<*y;i++)

That is because the contents of *y have not been assigned anything (at least in the code we can see). scanf() will have assigned a value to &y, which will have trashed the original value of y, which you then try to dereference (ie. *y)

If the second scanf() picks up the value, say, 1024, then your for() loop will be trying to dereference address 1024.

2012-04-04 02:29
by Andrew Edgecombe