简体   繁体   English

没有缓冲区大小的vswprintf()在小缓冲区而不是EOF上崩溃。 如何传递缓冲区大小

[英]vswprintf() without buffer size crashes on small buffer instead of EOF. How to pass buffer size

Using Borland C++ Builder 2009 使用Borland C ++ Builder 2009

I use vswprintf per RAD Studio's Help (F1) : 我根据RAD Studio的帮助(F1)使用vswprintf:

int vswprintf(wchar_t *buffer, const wchar_t *format, va_list arglist); 

Up to now I always provided a big buffer wchar_t OutputStr[1000] and never had any issues. 到目前为止,我一直提供一个大缓冲区wchar_t OutputStr[1000]并且从未出现任何问题。 As a test and wanting to do an improvement action, I tried a small buffer wchar_t OutputStr[12] and noticed that the program crashes entirely. 作为一项测试,想要做一个改进,我尝试了一个小缓冲区wchar_t OutputStr[12]并注意到程序完全崩溃了。 Even try{}catch(...){} doesn't catch it. 即使try{}catch(...){}也无法捕获。 Codeguard reports that a memcpy() fails, which seems to be the internal implementation. Codeguard报告说memcpy()失败,这似乎是内部实现。 I had expected an EOF as return value instead. 我原本以EOF作为返回值。

When searching online for vswprintf I find the c++ variant takes a buffer size as input, but I can't seem to convince my compiler to use that variant ? 在网上搜索vswprintf我发现c ++变体将缓冲区大小作为输入,但是我似乎无法说服编译器使用该变体? Any idea how to force it using BCB2009 ? 知道如何使用BCB2009强制它吗?

The whole point of the exercise was to implement a fall back scenario for when the buffer is too small in possibly one or two freak situations, so that I can allocate more memory for the function and try again. 练习的全部目的是实现一个后备方案,以防止在一个或两个怪胎情况下缓冲区太小,以便我可以为该函数分配更多的内存,然后重试。 But this mechanism doesn't seem to work at all. 但是这种机制似乎根本不起作用。

Not sure how to best test for the exact amount of bytes/characters needed either ? 不知道如何最好地测试所需的字节/字符的确切数量吗?

You can use vswprintf_s . 您可以使用vswprintf_s It returns negative value on failure 失败时返回负值

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

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