2
printf("%s%d%c", s, a, c);

The string would be printed on the stdout; but how the implementation knows to define what size of the char array to hold result?

Or the alternative way is: use the fixed size buffer, handle each variable argument behind the format string, if the interpreted string size exceed the buffer size, then output it to output stream. Is this true?

By the way, I think, the string type in C++, its implementation uses the heap memory, dynamic memory allocator, just as new, or delete, is this correct?

6
  • 4
    There are lots of open source implementations around. Why not just go look at one? Commented Jul 8, 2012 at 17:06
  • 5
    Why do you think that there is necessarily a "char array to hold the result"? Commented Jul 8, 2012 at 17:07
  • 1
    Maybe there isn't, and printf just writes one byte after another diectly to file descriptor 1... Commented Jul 8, 2012 at 17:08
  • There are many ways of implementing this, eg, just streaming the chars out, or reallocating if the buffer is too short. Commented Jul 8, 2012 at 17:11
  • 1
    Well, the c library I/O function is buffered, not write byte directly to stdout, that's the way of read/write sys call. Commented Jul 8, 2012 at 17:18

3 Answers 3

4

The string would be printed on the stdout

That's correct. Which is a stream, no char[] is required. Only sprintf() would require a guess at a string buffer size.

Sign up to request clarification or add additional context in comments.

3 Comments

The last sentence is slightly misleading since sprintf doesn’t take care of the buffer itself. The caller needs to know how large the buffer is. Of course they never do, they just use a fixed-size buffer. :p
Thanks for your answer. So Am I right about c++ string implementation, that the string is implemented as heap allocated memory by new/delete?
The std::string class is a library implementation detail, every compiler has its own library. Mine doesn't start using the heap until the string gets large enough. This otherwise has nothing to do with printf(), maybe you ought to ask another question about it.
1

As other people have explained, printf doesn't need to know the size of some output buffer ahead of time because it doesn't need to allocate one when writing to a FILE* stream.

However, you might then wonder: what about functions in the printf-family that don't write to FILE* streams? What about, say, the non-standard asprintf function that returns an allocated string?

The underlying implementation for the printf family of functions can do a dry-run. That is, it can simulate doing the operation without actually writing to memory and keep track of the number of chars that it would have written. Once it computes the that number, it then can allocate a buffer of the appropriate size and repeat the operation for real. This is what a caller would do when it needs to allocate a buffer itself, such as when calling snprintf.

(You also talk about buffered I/O. While printf can use that, printf doesn't necessarily know about the buffer itself (and it probably shouldn't). You can think of it as calling fputc a bunch of times, which then writes the char to some buffer, flushing it if it's full.)

Comments

0

but how the implementation knows to define what size of the char array to hold result

Just reads up to null terminating byte '\0'. No need to explicitely know the size of the "%s%d%c" which is passed in as a char *

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.