The array of char
can give better performance, after all will avoid a dynamic allocation of memory, which is something relatively expensive. No matter if the size will be determined at build or run time.
In the example of the question it would be better to use this form, but not always this is possible.
Safety and reliability
Security is always achieved by knowing what you are doing, understanding how the computer works, how the language works, the details of the API you are using, etc. The use of array in place of dynamic allocation neither helps nor harms security directly. There is nothing in a resource or other that prevents the overflow of memory usage. In C it is a programmer’s problem to treat this.
The use of dynamic memory is usually less reliable. Not by itself, but because programmers tend to err more in their use, and what is not so reliable may be less safe, but it is something indirect.
When to use
Normally the allocation of array is made in the stack - so it’s fast - which doesn’t allow you to have a array much large (much less if the size cannot be determined in the compilation or at least guaranteed that it will not be too large), nor that it survives at the end of a function (or scope), so there are cases that the allocation in the heap - with malloc()
- is the only viable solution.
Even in cases of allocation being in a structure, it can make it too big, and it is not always what you want, mainly because it would practically require your allocation in the heap (dynamic allocation), which is not always desirable.
Basically the difference between the array and dynamic allocation is really only in allocation, one offers no more resources than the other. Once allocated, it gives in the same, the language does not differentiate one from the other.
The default is always to use the simplest form, which is the allocation of array, unless there’s a reason to dynamically allocate. This is not premature optimization, on the contrary, dynamic allocation should be avoided whenever possible, whenever it does not bring specific problems. It is simpler, more reliable and faster, only has advantages when what it needs will not be limited by its characteristic.
Completion
Looking up, overall this code is poorly written and does not meet modern standards. Apparently it may have been written by those who do not yet know the right language, even by other errors.
See more about the decision (is C++, but essentially the same thing).
It’s just that I’m basically seeing a lot of these problems during the program, allocating the memory right at the beginning of the function, and not before using it. It has a function that has a char pointer that has in its contents, concatenated, basically a super string, which is received from reading a configuration file. It allocates the memory and already receives the reading function, only it is sent in the return of the function, so it is not displaced. This is generating a boring problem. How do I get the content into a non-dynamic variable and return that variable? It has to return char pointer.
– Juan Victor May da Rosa
To return has to be dynamic even, this is why there is this. Or you have to pass it allocated as parameter, so you need the value you allocate.
– Maniero