Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
You do not free
sdup
, therefore leak memory.res
is overwritten byasprintf
, so we need to save previous value ofres
to free memory.You never free
str
, therefore, leak memory.In case of
sz <= 0 || l == 0 || sz > l
you return static string which cannot befree
d. In other cases you return allocated string. This requires from caller additional checks whether to free the returned string or not, so it is not very convenient.In case of
strlen(s) < sz || sz <= 0
you return static string which cannot befree
d. In other cases you return allocated string. This requires from caller additional checks whether to free the returned string or not, so it is not very convenient.Initial value of
*lg == -1
is not documented.Several issues:
return *lg = 0;
-- return value is wrong.char **ret = malloc(sz * sizeof(char**));
-- not correct. Correct ischar **ret = malloc(sz * sizeof(char*));
arr
are freed afterdirReduc
call, the result will become invalid.This comment is hidden because it contains spoiler information about the solution
When you call
pop
, you shouldfree
the string you pop. Otherwise you get memory leak.What if someone
free
strings in the input array right afterdirReduc
call? The result will become invalid and if you will try to read the result, you will get undefined behavior. This cannot be "Best Practice" solution.Why do we need
strdup
here?if after realloc newRes is NULL, you will have:
Pair* solution = (Pair*) malloc(sizeof(Pair));
fails) or wrong solution because afterres = NULL;
you do not return or breakIt is not correct to compare floating point numbers for equality because of limited precision. More on the topic:
https://www.jetbrains.com/help/resharper/CompareOfFloatsByEqualityOperator.html
https://stackoverflow.com/questions/328475/should-we-compare-floating-point-numbers-for-equality-against-a-relative-error
Also floating point operations are slow. So it is better to check for division remainder via
a % b != 0
instead ofceil(a / b) != a / b
.// number of factors is not less than bits number
-- not correct. Correct:// number of factors is not greater than bits number
I know only one case when
goto
is useful in C: for cleanup (https://stackoverflow.com/a/245761). For all other cases Dijkstra's paper applies:When one uses
goto
, it becomes hard to see the control flow just by looking at code.Loading more items...