Talk:c/experimental/dynamic/asprintf
From cppreference.com
< Talk:c
In C, copying a string to itself is undefined behavior with most standard library functions, including for sprintf(). Regarding asprintf(), it should be easy enough to avoid any overlapping problems by first creating the new string on the heap, then printing to it, and finally assigning to the char** pointer. It seems to work with GLibC, but I'm not sure whether it's an implementation detail that should not be relied on. I cannot find the function's POSIX specifications, so I'm wondering if anyone has more information on this ? To be clear, I'm wondering if such usage is well-defined:
char *s = "abc"; asprintf(&s, "%s%s%s", s, s, s); printf("%s", s); // should print abcabcabc
It would be useful to mention this in the article if we find a source for this.