简体   繁体   English

C ++-当我使用sprintf()函数时程序崩溃(std :: cout正常工作)

[英]C++ - Program crashes when I use sprintf() function (std::cout works fine)

I am networking my program using RakNet and I want to get some network statistics during runtime. 我正在使用RakNet将程序联网,我想在运行时获取一些网络统计信息。 Luckily RakNet has two built in functions for this. 幸运的是,RakNet为此具有两个内置函数。 GetStatistics() and StatisticsToString() . GetStatistics()StatisticsToString() However, the program crashes when I call the StatisticsToString() function (StatisticsToString is just supposed to convert the statistics data into a string using the sprintf function) and I have absolutely no idea why... no error... nothing. 但是,当我调用StatisticsToString()函数(StatisticsToString仅应使用sprintf函数将统计数据转换为字符串StatisticsToString()时,程序崩溃,而且我绝对不知道为什么...没有错误...没有。 I modified the source code of the function and replaced the sprintf() function with std::cout and the program worked fine, just as intended. 我修改了该函数的源代码,并用std::cout替换了sprintf()函数,该程序按预期工作正常。

My custom function for writing stats: 我用于编写统计信息的自定义函数:

void HavNetProfiler::WriteStats(RakNet::RakPeerInterface *peer)
{

  if (RakNet::GetTimeMS() > nextStatTime)
  {
    nextStatTime = RakNet::GetTimeMS() + 1000;
    RakNet::RakNetStatistics rns;
    char *text;

    peer->GetStatistics(peer->GetSystemAddressFromIndex(0), &rns);
    RakNet::StatisticsToString(&rns, text, 0);
    //printf("%s\n\n", text);
  }

Snippet of source code from StatisticsToString() function: StatisticsToString()函数的源代码片段:

void RAK_DLL_EXPORT RakNet::StatisticsToString( RakNetStatistics *s, char *buffer, int verbosityLevel )
{
    if ( s == 0 )
    {
        sprintf( buffer, "stats is a NULL pointer in statsToString\n" );
        return ;
    }

    if (verbosityLevel==0)
    {
        sprintf(buffer,
            "Bytes per second sent     %" PRINTF_64_BIT_MODIFIER "u\n"
            "Bytes per second received %" PRINTF_64_BIT_MODIFIER "u\n"
            "Current packetloss        %.1f%%\n",
            (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_SENT],
            (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_RECEIVED],
            s->packetlossLastSecond*100.0f
            );

      //Modified std::cout solution
      /*std::cout << "\nB/s sent: " << (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_SENT] 
        << "\nB/s recieved: " << (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_RECEIVED]
        << "\nCurrent packetloss: " << s->packetlossLastSecond*100.0f << "\n"
        << std::flush;*/
    }

The std::cout solution is meant to be temporary as I would rather not have to edit the source code of RakNet. std::cout解决方案是临时的,因为我不想编辑RakNet的源代码。 I just need your help to figure out why sprintf can't work as intended. 我只需要您的帮助来弄清楚为什么sprintf无法按预期工作。 I am fairly new to C++ so maybe I am missing something really obvious here. 我对C ++相当陌生,所以也许我在这里遗漏了一些明显的东西。 I don't know... 我不知道...

Thanks! 谢谢!

sprintf(buffer,...) where buffer should be large enough to contain the resulting string. sprintf(buffer,...)其中buffer应该足够大以包含结果字符串。

You need something like: 您需要类似:

char text[1000];
RakNet::StatisticsToString(&rns, text, 0);

暂无
暂无

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

相关问题 当 new 运算符重载并输出到 std::cout 时,用 Clang 编译的 C++ 程序崩溃 - C++ program compiled with Clang crashes when new operator is overloaded and outputs to std::cout 当我使用 cout.tie(NULL) 时,程序不会为我的代码打印任何内容,但如果我打印 endl,程序运行正常 - When i use cout.tie(NULL), program doesn't print anything for my code, but if i print endl, program works fine 将代码链接到exe时C ++程序崩溃,但是将代码编译到exe时工作正常,为什么呢? - C++ program crashes when code is linked to exe but works fine when code is compiled into exe, how come? C++ 中的“error()”function 在我使用“#include”时有效<iostream> ',但不是'#include "std_lib_facilities.h"'</iostream> - 'error()' function in C++ works when I use '#include <iostream>', but not '#include "std_lib_facilities.h"' 在lldb中使用C ++ std :: cout - Use C++ std::cout in lldb 删除拒绝十六进制C ++程序的命名空间std特别是sprintf - removing namespace std specifically sprintf in a denary to hex C++ program Visual Studio C ++ 每当我尝试从[80] x [80]数组中循环一个cout时,程序就会崩溃 - Visual Studio C++ | Program crashes everytime I try to loop a cout from an array of [80]x[80] 为什么std :: cout适用于[] =“12345”而不适用于[] = {&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;}? - How come std::cout works fine for [] = “12345” but not for [] = {'1','2','3','4','5'}? 如何制作一个简单的C ++程序,其中没有刷新std :: cout - How to make a simple C++ program in which std::cout is not flushed 将带有std :: cout的C ++程序转换为NCurses / CDK - Convert C++ Program with std::cout to NCurses / CDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM