简体   繁体   English

printf的格式化缓冲区在哪里?

[英]Where is formatting buffer for printf?

I working on a constrained embedded system. 我在一个受限制的嵌入式系统上工作。

Presently we use snprintf to a buffer, then with another statement, print the buffer to the serial port: 目前我们将snprintf用于缓冲区,然后使用另一个语句将缓冲区打印到串口:

  char temp_buffer[256];
  int bytes_written = snprintf(temp_buffer, sizeof(temp_buffer),
                      "Solar system has %d planets\n",
                      10);
  if (bytes_written > 0)
  {
    Serial_Port_Output(temp_buffer, bytes_written);
  }

I want to switch to printf to write directly to the serial port. 我想切换到printf直接写入串口。 Per our compiler's documentation, I have intercepted the function call for outputting the data to use the serial port. 根据我们的编译器文档,我截获了函数调用以输出数据以使用串行端口。 (The interface uses block writing: an address and the number of characters). (接口使用块写入:地址和字符数)。

The printf function may use a character buffer for formatting, such as integer or floating point to text. printf函数可以使用字符缓冲区进行格式化,例如整数或浮点到文本。

Questions: 问题:

  1. Where is the buffer that printf uses for formatting? printf用于格式化的缓冲区在哪里? (Other inquiring minds want to know, before I make the changes.) (在我做出改变之前,其他有探究的人都想知道。)
  2. Is this a compiler (platform) dependent issue? 这是编译器(平台)依赖的问题吗?

Platform: Arm7tdmi processor, System On a Chip (SOC), IAR EW compiler. 平台:Arm7tdmi处理器,片上系统(SOC),IAR EW编译器。

This is completely implementation-specific. 这完全是针对特定于实现的。 printf is under no obligation to use any buffer. printf没有义务使用任何缓冲区。 Of course it has at its disposal the stdio buffer associated with the FILE ( stdout in the case of printf ) but that may be zero-length if the program turned off buffering with setbuf / setvbuf . 当然,它具有与FILE相关的stdio缓冲区(在printf的情况下为stdout ),但如果程序使用setbuf / setvbuf关闭缓冲,则可能为零长度。 It's also possible that printf has an internal buffer; printf也有可能有一个内部缓冲区; for a proper C implementation this would need to have automatic storage ("on the stack") but a low-quality embedded one without threads might use a static buffer. 对于正确的C实现,这需要具有自动存储(“在堆栈上”),但是没有线程的低质量嵌入式可能使用静态缓冲区。 In any case, printf is specified to work as if by repeated calls to fputc , and it could certainly be implemented this way without any buffer at all. 在任何情况下, printf被指定为通过重复调用fputc来工作,并且它当然可以以这种方式实现而没有任何缓冲区。

It is library rather than compiler dependent and you should consult the library's documentation and possibly where available the source code. 它是库而不是编译器,您应该查阅库的文档以及可能的源代码。 This (perhaps out-of-date) IAR C Library documentation says: 这个(可能是过时的) IAR C库文档说:

Since a complete formatter demands a lot of space there are several different formatters to choose between. 由于完整的格式化程序需要大量空间,因此可以选择几种不同的格式化程序。 For more information, see the see the IAR C Compiler Reference Guide. 有关更多信息,请参阅“IAR C编译器参考指南”。

The current IAR compiler reference discusses formatter selection, though in most cases the linker can automatically select the most appropriate formatter. 当前的IAR编译器参考讨论格式化程序选择,但在大多数情况下,链接器可以自动选择最合适的格式化程序。 The documentation even discusses further optimisation available if rebuilding the library (for which you presumably need a source license). 如果重建库(可能需要源许可证),文档甚至讨论了可用的进一步优化。

Some implementations (not specifically IAR) use significant stack space. 某些实现(不是特定的IAR)使用大量的堆栈空间。 If you want full control, you might consider using an open-source implementation such as Tiny printf . 如果您想要完全控制,可以考虑使用Tiny printf等开源实现。 It is not a complete ISO implementation, but suitable for many embedded applications. 它不是完整的ISO实现,但适用于许多嵌入式应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM