Ad
  • Custom User Avatar

    The 'numbers test' in C has strings made out of ASCII numbers that are randomly generated. This is tested for the fact that numbers or non-alphabet characters should be ignored in the function. If you're geting 15 lines of 10 random numbers from printf() or puts(), everything's alright.

  • Custom User Avatar

    Nice insight, didn't think of that.

    My solution was a clear case of "Wait, it works, but what about the memory" - and I chose the path of least resistance and brain usage.

    I also agree about leaving the allocating to the caller - but this is not (?) realizable within this Kata.

  • Custom User Avatar

    Most solutions I find to this in C seemingly are inefficient with memory. They allocate memory according to the length of the source string, and make no effort to clean up the memory later. I'm primarily an embedded dev, so I'm very concerned with memory use if I can.

    My solution cleans up the memory afterwards by duplicating the string (that's terminated, but has too much memory allocated to it) to another pointer, freeing the original (longer than necessary) string and returning the size-optimized string.

    What's your opinion on this? Of course, if this is all used in a local environment that temporarily exists, which properly frees memory by some mechanism after it's done it's job. Then it's no problem. But if this function is run many times without freeing memory, my cleanup will use up less memory in exchange for a bit of processing time.

    What's best practice? Assume that output strings are only used temporarily and freed later, or make the strings effectively use space at the expense of processing time?