2

I wrote an function which should realloc the array for each input. I think that function works well, but when I'm trying to work with the array I got segmentation fault.

Input:

[1,2] [6,3] [2.5,3.5]

I have to check if the user enters input in the correct form '[' number ',' number ']'. I have to have at least 2 entries. The end of input isn't new line, but EOF (CTRL+D or CTRL+Z).

My code:

double ** allocation (double ** field, int i)
{
   double x = 0, y = 0;
   char obr[1], cbr[1], col[1];

   while (scanf("%c%lf%c%lf%c", &obr, &x, &col, &y, &cbr) == 5)
   {
       if (col[0] != ',' || obr[0] != '[' || cbr[0] != ']')
           return 0;

       field = (double **) realloc(field, (i + 1) * sizeof(*field));
       if (field == NULL)
           return 0;

       field[i] = (double *)malloc(2 * sizeof(double));
       if (field[i] == 0)
           return 0;

       field[i][0] = x;
       field[i][1] = y;
       i++;
  }
  if (feof (stdin))
      return 0;

  return field;
}

And when I want to use this:

double min = sqrtf(powf(field[0][0] - field[1][0], 2) + powf(field[0][1] - field[1][1], 2));

I will get my segmentation fault.

Grim
  • 37
  • 2
  • 10
  • 2
    Is the issue that your `allocation` function returns a value of `1` while it's supposed to return a pointer to a `double` array? – Jongware Nov 30 '13 at 10:22
  • 1
    (In addition) I guess the problem is that you modify `field` *locally* (by using `realloc`), and its new value gets discarded when you leave the function. – Jongware Nov 30 '13 at 10:25
  • maybe `return 1;` -> `return field;` – BLUEPIXY Nov 30 '13 at 10:31
  • all of you have right, but i tried do math function in function, same effect(segmentation fault), anyway I'll try to modify it. – Grim Nov 30 '13 at 11:36
  • I added `field = (double **) malloc((i + 1) * sizeof(*field));` before while, but still doesn't work, could somebody help me please? – Grim Nov 30 '13 at 17:18
  • If `realloc` returns `NULL`, you will have leaked whatever memory was previously allocated since you are overwriting `field`. – pat Sep 23 '14 at 04:49

1 Answers1

0

From man realloc (emphasis mine):

The realloc() function tries to change the size of the allocation pointed to by ptr to size, and returns ptr. If there is not enough room to enlarge the memory allocation pointed to by ptr, realloc() creates a new allocation, copies as much of the old data pointed to by ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory.

You're not guaranteed to be using the same memory when you use realloc, you need to pass the pointer back for the case that it's been moved to a new location. You can check for success by checking whether it's a NULL pointer or not, too.

tl;dr: You need to use return field instead of return 1 or return 0.

Leigh
  • 12,038
  • 4
  • 28
  • 36
  • Sorry, I'm beginner and I didnt get your hint. – Grim Nov 30 '13 at 11:37
  • No, my fault. You need to use `return` to pass it back, since the copy of `field` that you get back once you use `realloc` is "lost" as soon as you use `return` and leave the function. – Leigh Nov 30 '13 at 12:00
  • It still doesn't solve my problem. And I don't know why this ends by hitting `Enter`. – Grim Nov 30 '13 at 12:14