Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在sscanf()之后调用free()时检测到堆损坏_C++_C - Fatal编程技术网

C++ 在sscanf()之后调用free()时检测到堆损坏

C++ 在sscanf()之后调用free()时检测到堆损坏,c++,c,C++,C,我正在尝试编写一个函数,将十六进制值字符串转换为字节数组。这个代码怎么了 调用free()后,检测到错误堆损坏。如果我给sscanf打电话,一切正常。sscanf是否写入超出malloc分配的内存的内容 unsigned char* hextobytes(const string& hex) { size_t size = hex.size() / 2; unsigned char* bytes = (unsigned char*)malloc(size); co

我正在尝试编写一个函数,将十六进制值字符串转换为字节数组。这个代码怎么了

调用free()后,检测到错误堆损坏。如果我给sscanf打电话,一切正常。sscanf是否写入超出malloc分配的内存的内容

unsigned char* hextobytes(const string& hex) {
    size_t size = hex.size() / 2;
    unsigned char* bytes = (unsigned char*)malloc(size);
    const string::value_type* pos = hex.c_str();

    for (size_t c = 0; c < size; c++) {
        sscanf((pos + 2 * c), "%2hhx", bytes + c);
    }

    return bytes;
}

int _tmain(int argc, _TCHAR* argv[]) {
    string hex = "FFFF";
    unsigned char* bytes = hextobytes(hex);
    free(bytes);

    return 0;
}
无符号字符*十六进制字节(常量字符串和十六进制){
size\u t size=hex.size()/2;
无符号字符*字节=(无符号字符*)malloc(大小);
常量字符串::value_type*pos=hex.c_str();
对于(大小c=0;c

更新:我正在研究VisualSudio 2013

< P>你的源代码的主要问题是它是C++,但是编程非常的C风格。 其他人指出,发布的代码没有显示您声称的错误

但请允许我展示一下C++如何使用这种方式,不可能有任何的堆损坏,因为C++提供了我们需要的所有工具来避免“裸”指针:

#include <string>
#include <vector>
#include <iostream>

std::vector< unsigned char > hextobytes( const std::string & hex )
{
    std::vector< unsigned char > rc;
    for ( size_t i = 0; i < hex.size(); i += 2 )
    {
        // this may throw std::invalid_argument if no
        // conversion can be performed
        // formally std::out_of_range would be also a
        // possibility, but not with a two-digit hex...
        rc.push_back( static_cast< unsigned char >(
            std::stoul( hex.substr( i, 2 ), 0, 16 ) )
        );
    }
    return rc;
}

int main()
{
    std::string hex( "FFFF" );
    std::vector< unsigned char > bytes = hextobytes( hex );
    for ( auto a : bytes )
    {
        std::cout << static_cast< int >( a ) << "\n";
    }
    return 0;
}
#包括
#包括
#包括
标准::向量<无符号字符>十六进制字节(常量标准::字符串和十六进制)
{
std::vectorrc;
对于(大小i=0;i(
标准:斯多尔(十六进制子序列(i,2,0,16))
);
}
返回rc;
}
int main()
{
标准:字符串十六进制(“FFFF”);
std::vectorbytes=hextobytes(十六进制);
用于(自动a:字节)
{
我已经找到答案了

Microsoft版本的scanf不支持使用长度修饰符“hh”指定无符号字符。它支持使用修饰符“h”指定短整型


使用一个短数组代替非符号字符解决了我的问题。

在C++中不要使用<代码> MalOC ,使用<代码>新< /C> >(并且尽量避免使用它:读关于智能指针和容器)Hm.…这是非常多的C,我必须努力去看它的C++部分……@ DeVixe更仔细地看,它在那里,在for循环的第三个表达式中):Lundin:他正在铸造<代码> MalCube()的返回值< /C> >,这是一个死赠品。此代码中没有错误。请记住,堆损坏通常在损坏发生后很久才会报告,并且通常不是由进行分配的代码引起的。不幸的是,Microsoft的既定政策是不必继续更新编译器的C功能。这是放弃C风格而放弃的另一个原因对于正确的C++习语,@虽然不能解决实际问题,但我会保留你的C++风格的例子,谢谢你,这取决于你如何定义“实际问题”。我在C++环境中考虑C风格编码,特别是裸指针和数组的使用,这是个问题,因为很多问题根本不会在适当的C++中出现。(当然,它有自己的问题集合,但你知道我来自哪里)。