Ad
  • Custom User Avatar

    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().

  • Custom User Avatar

    the return must be a pointer to a (free-able) dynamically allocated string

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution