I'm getting a memory leak error in my own substr function
The key to solving this would be to add a guard to the beginning of your function, before you allocate for subs
which may never be returned and thus be unable to be freed.
if (!s) return NULL;
You can still check later when subs
is allocated and return NULL
if that allocation fails.
subs = (char *)malloc(sizeof(char) * (len + 1));
if (!subs || !s)
{
return (NULL);
}
I the sub-statement of the if statement will get the control when s is equal to NULL when there will be a memory leak.
Pay attention to that you may remove the check whether s is a null pointer as all C standard string functions do. If the user will call the function passing a null pointer then there will be undefined behavior.
And the call of malloc can allocate more memory then it is required.
Also the types of the second and third parameters are inconsistent.
char *ft_substr(char const *s, unsigned int start, size_t len)
The both should have the type size_t
.