If I need to copy a string, src
into the array, dest
, using strcpy
or strncpy
, should I allocate an arbitrarily large sized array (like char dest[1024]
for example) or should I calculate the size of src
using something like strlen
and then dynamically allocate a block using malloc (strlen(src) * sizeof(char))
.
The first approach seems "easier" but wouldn't I be consuming more space than I needed? And in some cases it might even fall short. On the other hand, the second approach seems more precise but tedious to do every time and I would have to deallocate the memory everytime.
Is this a matter of personal taste or is one of the above preferable over the other?