Malloc

sush -  
 chwepsy -
Hello,

I am starting to study the malloc function and it looks like Chinese to me... For example, to create a function that returns an array of ints, which contains all int values from min to max, if min is greater than max, a pointer will return 0:
from what I understand, I need to define the value of malloc (#define LEN), but since there are int max and min already defined in the function, I’m not sure what to put after LEN?

I tried to start the function but I’m completely stuck... Thank you in advance for your help.

#define LEN ?

int *ft_range(int min, int max)
{
int min = 0;
int max = n;
char *str;
str = malloc(sizeoff(*str)
if (min > max)
{
str = NULL;
}
}

12 réponses

juliencolin54 Posted messages 238 Status Membre 55
 
Hello,

Here is a commented code that should meet your expectations:
int *ft_range(int min, int max)
{
int *arr; // Future int array
int i;

if (min > max) // if min > max ; return 0
return (0);
arr = malloc(((max - min) + 2) * sizeof(int));
/*
** malloc <=> I allocate (((max - min) + 2)) slots that have the size of an int
** For an array that goes from 1 to 3;
** that makes (3 - 1) + 2 = 4 slots
** (from 0 to 3 + one last to store null
*/
for (i = 0; i < ((max - min) + 1); i++) // filling the array with the right values
arr[i] = min + i;
arr[i] = 0; // Last slot of the array is null to detect its end
return (arr);
}

int main(int ac, char **av)
{
int *arr;

arr = ft_range(5, 10); // assigning the array with min and max values; 5 & 10
if (arr)
while (*arr) // Displaying the array slot by slot
printf("%d\n", *(arr++));
return (0);
}

--
Software is like sex, it's better when it's free - Linus Torvald
5
amn
 
It's the same as for the first one, right? Except that when min > max, it will be a range pointer that will be NULL, and then to return the size of the range, we write this? arr = malloc(range) * sizeof(int))

And for the first if when min > max, return 0, since it’s a null pointer that will be returned, why don’t we use a pointer?
1
chwepsy
 
42 spotted ;D

In the end, it's a great tutorial, thank you.
1
amn
 
Hello, thank you! It's very well explained, it's much clearer like that. Just one thing for min > max, it doesn't return 0, but null, so should I replace 0 with NULL? And it is supposed to return a null pointer, so it should have the asterisk (*) of the pointer, right?

After that, I have almost the same exercise, except that instead of returning an array, it allocates and assigns an array of int; it will be the same thing, right? In this exercise, the function will have an extra 'int' (int **range), and this will point to NULL if min > max, then the size of range will be returned, will it look like this?
if (min > max)
range = NULL;
arr = malloc(range * sizeof(int));
0
juliencolin54 Posted messages 238 Status Membre 55
 
In fact, NULL is just a macro like any other.
It’s the preprocessor that will automatically replace it in your code with
((void *)0) or ((char *)0) (depending on your architecture), which is 0.
As a reflex, I use 0 instead of NULL, as it saves space.
It's up to you to get into the habit you want. The main thing is to understand it and to be able to explain why you use 0 instead of NULL, and how it is "equivalent."

For your second exercise, would the function prototype be
void ft_range(int **arr, int min, int max)
?

Software is like sex, it's better when it's free - Linus Torvald
0
amn
 
Hi yes that's right :)
0
juliencolin54 Posted messages 238 Status Membre 55
 
Do you have any idea how to do it, or have you tested something?

--
Software is like sex, it's better when it's free - Linus Torvald
0
juliencolin54 Posted messages 238 Status Membre 55
 
It's the same as for the first one, right? Except that when min > max, it will be a range pointer that is NULL, and then to return the size of the range, we put this? arr = malloc (range) * sizeof(int)) 
--> Not quite, as you confirmed to me earlier, the function won't return anything (void).
I'll let you think a bit and post working or non-working code, so I can tell you what's wrong.

And for the first if when min>max, return 0, since it's a null pointer that will be returned, why don't we use a pointer?
--> Didn't I explain it to you two posts ago? NULL is a pointer... which is equal to 0.

--
Software is like sex, it's better when it's free - Linus Torvald
0
amn
 
Désolé, je ne peux pas vous aider avec ça.
0
juliencolin54 Posted messages 238 Status Membre 55
 
Is it supposed to be the code for your first or second exercise?

--
Software is like sex, it's better when it's free - Linus Torvald
0
amn
 
Second, if you ask for that it means it's not good lol, but from what I understand it's almost the same thing, we’re just going to change the range pointer which returns null and return its value instead?

Sorry for the response time, I’m doing the 42 pool, we see a lot of things at the same time and I don’t know where to turn my head.
0
juliencolin54 Posted messages 238 Status Membre 55
 
No worries,

Actually, I don't know what your range pointer is, is it your double array of integers?

--
Software is like sex, it's better when it's free - Linus Torvald
0