Janis Papanagnou
2024-12-19 01:30:18 UTC
Inspecting some of my "C" source code here I noticed a construct that
I dislike...
static char buf[32]; // sufficient space for ansi sequence
...
sprintf (buf, "\033[38;5;%dm%c\033[0m", ...);
In case of known or deducible string sizes I'm preferring and using
some of the alloc() functions. In the sample I can at least deduce an
upper-bound for the buffer-size. But it's anyway still an undesired
hard-coded buffer size that I'd like to avoid.
I recall that in C++ I used "String-Streams" for dynamically extended
strings. But in "C" my reflex (and habit) is to write things like the
above code.
Is there something in "C" that allows dynamic flexibility of strings?
(Or are there other options that an experienced "C" programmer would
use instead?)
(If there's something available only with newer C-standards I'd also
appreciate a hint.)
Janis
I dislike...
static char buf[32]; // sufficient space for ansi sequence
...
sprintf (buf, "\033[38;5;%dm%c\033[0m", ...);
In case of known or deducible string sizes I'm preferring and using
some of the alloc() functions. In the sample I can at least deduce an
upper-bound for the buffer-size. But it's anyway still an undesired
hard-coded buffer size that I'd like to avoid.
I recall that in C++ I used "String-Streams" for dynamically extended
strings. But in "C" my reflex (and habit) is to write things like the
above code.
Is there something in "C" that allows dynamic flexibility of strings?
(Or are there other options that an experienced "C" programmer would
use instead?)
(If there's something available only with newer C-standards I'd also
appreciate a hint.)
Janis