You cannot safely add to an arbitrary string, because, firstly, string constants tend to be in constant memory, so trying to write them can lead to a segmentation error, and secondly, you donβt have to guarantee that if they passed you a buffer that you did not shoot at the end of it.
In particular, if you do this, char x [500];
there is no guarantee that strlen (x) will return 500 to you. It will return to you how many characters it needs to count from the beginning of x before it reaches zero. It can return you 0, 1 ... 500, 501 ..., depending on what is in x.
Indeed, your only options are to call append with the size of the buffer you add (so that you can do something suitable if the buffer is full) or make append allocate a new buffer each time it is called, in which case, of course, you need to free the buffer.
Tom tanner
source share