Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Bugs: int **range = 0; should be int *range; and then call the function like ft_ultimate_range(&range, min, max);. That's the whole reason the parameter is pointer-to-pointer, so that you c...
Answer
#2: Post edited
- Bugs:
- - `int **range = 0;` should be `int *range;` and then call the function like ` ft_ultimate_range(&range, min, max);`. That's the whole reason the parameter is pointer-to-pointer, so that you can return a malloc:ed pointer through a parameter.
- - `while (min < max)` neither min nor max change inside the loop. This is the reason for the seg fault, because `i` keeps increasing and eventually you access `range` out of bounds.
- Minor stuff:
- - There's no obvious reason why you can't simply return the pointer from the function. It then becomes `int* ft_ultimate_range(int min, int max)` and all the pointer-to-pointer syntax can be removed.
- - Don't give a plain `int` the name `ptr`...
- - `lentgh` typo -> `length`
- Bugs:
- - `int **range = 0;` should be `int *range;` and then call the function like ` ft_ultimate_range(&range, min, max);`. That's the whole reason the parameter is pointer-to-pointer, so that you can return a malloc:ed pointer through a parameter.
- - `while (min < max)` neither min nor max change inside the loop. This is the reason for the seg fault, because `i` keeps increasing and eventually you access `range` out of bounds.
- - `while(i < max - min)` in main() doesn't make any sense, you can simply return that loop. You probably intended to do something with the allocated data instead here?
- Minor stuff:
- - There's no obvious reason why you can't simply return the pointer from the function. It then becomes `int* ft_ultimate_range(int min, int max)` and all the pointer-to-pointer syntax can be removed.
- - Don't give a plain `int` the name `ptr`...
- - `lentgh` typo -> `length`
#1: Initial revision
Bugs: - `int **range = 0;` should be `int *range;` and then call the function like ` ft_ultimate_range(&range, min, max);`. That's the whole reason the parameter is pointer-to-pointer, so that you can return a malloc:ed pointer through a parameter. - `while (min < max)` neither min nor max change inside the loop. This is the reason for the seg fault, because `i` keeps increasing and eventually you access `range` out of bounds. Minor stuff: - There's no obvious reason why you can't simply return the pointer from the function. It then becomes `int* ft_ultimate_range(int min, int max)` and all the pointer-to-pointer syntax can be removed. - Don't give a plain `int` the name `ptr`... - `lentgh` typo -> `length`