-1

I have a segment fault int this exercice.

Instruction:

• Write an ft_ultimate_range function which allocates and assigns an int array. This int table will contain all values ​​between min and max.
• Min included - max excluded.
• If the min value is greater than or equal to the max value, range will point to NULL.
• The range size will be returned (or 0 in the event of a problem).

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

int ft_ultimate_range(int **range, int min ,int max)
{
    int len;
    int i;

    i = 0;
    len = max - min;
    if (min >= max)
    {
        *range = NULL;
        return(0);
    }

    **range = (int)malloc(sizeof(int) * len);
    while (min < max)
    {
        range[0][i] = min;
        i++;
        min++;
    }
    return(len);
}

int main()
{
    int min;
    int max;
    int **range = NULL;

    min = 0;
    max = 10;
    printf("%d\n", ft_ultimate_range(range, min, max));
    return(0);
}

Myno
  • 158
  • 2
  • 13

2 Answers2

1

As you have guessed your allocation is wrong

by writing

**range = (int)malloc(sizeof(int) * len);

you meant to assign to the dereference of the dereference of range (NULL)

range = NULL *range = whatever value is at offset 0 **range = whatever value is at the address from the value read at address 0

please read this for writing the allocator

it would boil down to:

array = malloc( sizeof(int[rows][cols]) );

but in your case you want to return the array as a pass by ref var

int main() {
    int **array;
    ft_ultimate_range(&array)
}

int ft_ultimate_range(int ***array) {
   *array = malloc( sizeof(int[rows][cols]) );
}

BUT

your code is kind of not using the full 2d array and the function is not returning it:

    while (min < max)
    {
        range[0][i] = min;
        i++;
        min++;
    }

you are only using one row here, and not even using the data in range after that, one would think the code to be irrelevant, as you are only returning len.

dvhh
  • 4,724
  • 27
  • 33
  • 1
    They do not really want a 2D array, just a pointer to a 1D array. Initially, they want an `int *` to point to where the data will be stored, and then they need an `int **` so the function can pass the `int *` back. `range[0][i]` is just a way of writing `(*range)[i]`. – Eric Postpischil May 19 '20 at 16:14
  • the author code is confusing in its intent, might be a work in progress, and just placeholder loop – dvhh May 19 '20 at 16:16
  • 1
    No, we saw this in another question recently. These students are working on an assignment that requires them to return an array of ‘int` via an `int **` parameter. – Eric Postpischil May 19 '20 at 16:18
  • Thanks or the insight @EricPostpischil – dvhh May 19 '20 at 16:24
1
**range = (int)malloc(sizeof(int) * len);

This line raises the warning when i compile your code:

warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]

Never ignore the warning from the compiler.

malloc function returns a pointer, why do you cast it to int value ?

You want 2D array, but i seems that is matrix 1 x len, it's 1D array (vector).

OK, forget the 2D or 1D array (vector). If you want to use double pointer in your code, you have allocate for it first.

// this line should be in main function
range = malloc(sizeof(int *));
if(!range) {
   return -1;
}
// this line before while loop
range[0] = malloc(sizeof(int)*len);
if(!range[0]) {
   return -1;
}

If you want a matrix "M*N", you can allocate the pointer as below:

// allocate double pointer
range = malloc(sizeof(int *)*M);
if(!range) {
   return -1;
}

// then using a loop to allocate the pointers:
for(int i = 0; i<N; i++) {
   range[i] = malloc(sizeof(int)*N);
   if(!range[i]) {
     return -1;
   }
}

Do not forget to free the pointer when you do not need to use (free(range[i] then free(range)).

Hitokiri
  • 3,607
  • 1
  • 9
  • 29