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.
Based on my understanding, it is a difference of stack variable vs heap variable.
char res[len + 1]
would make a local variable on the stack.There are two problems with this. 1) it is a local variable, so it can't be easily seen or used outside of the function. 2) it is on the stack, and the compiler needs to know ahead of time exactly how much space to put on the stack.
If you don't know ahead of time how much space you need, the variable must be allocated on the heap.
char *res = malloc(len + 1);
creates a space on the heap.Since we don't know ahead of time how long the input DNA sequence is going to be, the program will only work if you allocate it on the heap.
Also, the test suite that "uses" our code expects that we allocated on the heap, so it will try to
free()
.the return must be a pointer to a (free-able) dynamically allocated string
This comment is hidden because it contains spoiler information about the solution