Timeline for Avoiding repetition when using the "snprintf idiom" to write text
Current License: CC BY-SA 4.0
18 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 29, 2019 at 11:15 | vote | accept | Carcigenicate | ||
Jun 29, 2019 at 0:49 | comment | added | Peter Cordes |
Oh, I'd forgotten that asprintf didn't return the pointer; yeah that's clunky. In C++ if knowing the length was useful I'd say use explicit-length std::string, but C doesn't make that as easy.
|
|
Jun 29, 2019 at 0:41 | comment | added | Jerry Coffin |
@PeterCordes: Except that it's portable, with uniform behavior (neither of which is true of asprintf , I'm afraid). And it has a better interface than asprintf (IMO, obviously).
|
|
Jun 29, 2019 at 0:00 | comment | added | Peter Cordes |
@Mark: yeah, was about to say: congratulations, you've invented asprintf .
|
|
Jun 28, 2019 at 21:31 | comment | added | Mark |
Call the function asprintf , and wrap it in an #ifdef so it only gets compiled on Windows.
|
|
Jun 28, 2019 at 18:49 | comment | added | Deduplicator |
Interesting figures, and I expected no significant difference betwee them.Also, I was aware those platforms where va_end() is actually needed (and the other two actually expensive), are a bit exotic.
|
|
Jun 28, 2019 at 18:40 | comment | added | Jerry Coffin | @Deduplicator: In theory they can, but on a reasonably modern machine, they're typically pretty lightweight. For example: godbolt.org/z/7qxhlT. gcc compiles va_start to slightly less code than va_copy--but clang compiles va_copy to slightly less code (and msvc/x86 compiles them to two instructions apiece, though its rendition of va_copy may be marginally faster, since it's two movs, vs an lea and a mov for va_start). Bottom line: unless you're doing compiler-specific optimization,it's not immediately obvious to me that you're likely to gain much from refactoring this. | |
Jun 28, 2019 at 16:35 | comment | added | Deduplicator |
Also, va_copy() and va_start() can do a non-trivial amount of work and even allocate resources. Which is the reason va_end() is needed at all. Yet another reason to refactor your to_string() .
|
|
Jun 28, 2019 at 16:27 | comment | added | Jerry Coffin | @Deduplicator: Good question. The honest answer is that I don't know of much reason to prefer one over the other in this case. | |
Jun 28, 2019 at 15:52 | comment | added | Deduplicator |
Why use va_copy() instead of a second va_start() and re-using args ?
|
|
Jun 28, 2019 at 15:44 | history | edited | Jerry Coffin | CC BY-SA 4.0 |
edited body
|
Jun 28, 2019 at 0:32 | history | edited | Jerry Coffin | CC BY-SA 4.0 |
deleted 9 characters in body
|
Jun 27, 2019 at 22:48 | history | edited | Jerry Coffin | CC BY-SA 4.0 |
deleted 17 characters in body
|
Jun 27, 2019 at 22:39 | history | edited | Jerry Coffin | CC BY-SA 4.0 |
deleted 17 characters in body
|
Jun 27, 2019 at 22:37 | vote | accept | Carcigenicate | ||
Jun 27, 2019 at 22:37 | |||||
Jun 27, 2019 at 22:17 | history | edited | Jerry Coffin | CC BY-SA 4.0 |
added 1090 characters in body
|
Jun 27, 2019 at 22:10 | history | edited | Jerry Coffin | CC BY-SA 4.0 |
added 1090 characters in body
|
Jun 27, 2019 at 22:02 | history | answered | Jerry Coffin | CC BY-SA 4.0 |