On Tue, Feb 22, 2022 at 10:01:47PM +0100, Emile Michel Hobo via Gcc wrote: > Dear developers: > > I find it counterintuitive that if I repeatedly reset a variable by using > strcpy with an empty string "" to that variable and then us strcat to add > characters to that variable that that seems to lead to a stack overflow. > > I would expect strcpy to first free the variable, then malloc, then copy the > string value into the variable. I think that would be a better > interpretation, because it can keep running for quite some time before it > overflows and doesn’t really call it.
strcpy() doesn't dynamically allocate memory according to docs. And this great. Otherwise we will have a performance impact. It would be terrible. For example, i allocate some buffer once at program initialization and later do many strcpy() with it. This is a frequent scenario. > I ended up reimplementing the reset function, implementing it with free and > malloc myself, but the way strings have been implemented in C is highly > counter-intuitive. In general pointers tend to be bug-prone, but here you > would expect this not to happen. Implementing string manipulation routines for your needs is ok. Strings in C are very intuitive. You work mostly without allocations and docs specify this. This is great and give the best performance where we need it. Where you don't need it - you make string manipulation routines for yourself. -- Олег Неманов (Oleg Nemanov)