代码之家  ›  专栏  ›  技术社区  ›  serg.nechaev

libmicrohttpd在html响应中发送垃圾(const char*vs std::字符串.数据() )

c++
  •  1
  • serg.nechaev  · 技术社区  · 7 年前

    当我使用 std::字符串的数据()

    工作正常:

        const char* html2 = "Free for personal and commercial use under the CCA 3.0 license";
        response = MHD_create_response_from_buffer(strlen(html2), (void *) html2, MHD_RESPMEM_PERSISTENT);
    

    enter image description here

    发送垃圾邮件:

        std::string html = "Free for personal and commercial use under the CCA 3.0 license";
         response = MHD_create_response_from_buffer(strlen(html.data()), (void *) html.data(), MHD_RESPMEM_PERSISTENT);
    

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  2
  •   Michael Veksler    7 年前

    阅读 documentation :

    缓冲区的内存管理选项;使用MHD\u RESPMEM\u PERSISTENT if 缓冲区是静态/全局内存,如果 如果缓冲区在瞬时存储器中(即打开),MHD\U RESPMEM\U必须复制 必须由MHD复制

    MHD_RESPMEM_MUST_COPY 而不是 MHD_RESPMEM_PERSISTENT string::data()

    请注意 char* “文字” “,所以 适用于您的第一个变体。但在第二种变体中,绝对不是“坚持” ". 第二种变体更符合 “缓冲区在临时内存中(即在堆栈上)” ,所以 MHD\u RESPMEM\u必须复制