[lug] An unusual C construct
Anthony Foiani
tkil at scrye.com
Tue Sep 21 22:26:47 MDT 2010
Bear Giles <bgiles at coyotesong.com> writes:
> The point is to use snprintf(), not sprintf(), where the second
> argument is the size of the buffer you're writing into. That
> prevents buffer overflows. You should still explicitly set the last
> char to \0 since the function doesn't.
Sure. I didn't mean to nit-pick; it just took me a bit to read
through your examples to figure out what you were doing.
Anymore, I do most of this type of work in C++; using std::string
means that I can pretty much ignore all these issues ('\0', storage
space, etc). Used sensibly, std::string is almost as fast, too --
maybe even faster.
(Consider a typical "copy some string" situation; with plain
zero-terminated C strings, it takes two passes to do it correctly.
Since std::string keeps a length field alongside the data, it can
correctly size the destination without traversing the data.)
Anyway.
Happy hacking,
t.
More information about the LUG
mailing list