if this is a bad idea, how to allocate memory in the function?
There are certain patterns to manage memory:
Keep consistency in mind, or else it will easily lead to a memory leak or dangling pointers.