- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有将十六进制字符串转换为字节数组的函数,
BYTE* HexStrToByteArray(std::wstring hex_str)
{
int len = hex_str.size()*0.5f;
BYTE *bytearray = new BYTE[len];
for(int i = 0; i < len; i++)
{
swscanf(hex_str.c_str() + 2*i, L"%02x", &bytearray[i]);
}
return bytearray;
}
在代码中我是这样使用的
BYTE *byte_array = HexStrToByteArray(hex_str);
现在函数工作正常,但是当我尝试释放内存时,在函数中分配
delete [] byte_array;//HEAP CORUPTION ERROR
我有 HEAP CORUPTION 错误...我做错了什么?
最佳答案
在您的代码上运行 valgrind
很快就会发现问题:
Invalid write of size 4
==34783== by 0x21B719: swscanf
Address 0x100004000 is 0 bytes inside a block of size 3 alloc'd
当我在长度为 6 的十六进制字符串上运行您的代码时出现此错误:
BYTE *ba = HexStrToByteArray(L"123456");
当然,这应该产生 3 个字节。但是,%02x
转换说明符使 swscanf()
期望指向 unsigned int
的指针(在您的实现中恰好是 4 个字节长) ,而您传递给它的是 BYTE *
,它可能是指向 unsigned char
的指针。
与其尝试乱用 scanf()
,这很糟糕,不如使用 strtoul()
。此外,使用 std::size_t
作为大小,请不要用 float 污染整数运算。此外,如果您的十六进制字符串是奇数个字符,您将需要为其分配一个额外的字节。此外,我建议您通过 const 引用传递输入字符串,以避免不必要的复制。
BYTE *HexStrToByteArray(const std::wstring &hex_str)
{
std::size_t len = (hex_str.size() + 1) / 2;
char buf[3] = { 0 };
BYTE *bytearray = new BYTE[len];
for (std::size_t i = 0; i < len; i++) {
buf[0] = hex_str[2 * i + 0];
buf[1] = hex_str[2 * i + 1];
bytearray[i] = std::strtoul(buf, NULL, 16);
}
return bytearray;
}
关于c++ - 为什么我在这个简单的代码中有 HEAP CORRUPTION?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21016291/
void *insert_rear_node(void *arg) { int *argument=(int *)arg; int value=*argument; //Assume
嘿,我的代码在执行后说“帐户”已损坏... 这是什么意思,我该如何解决? #include #include using namespace std; class BankAccount { pr
我正在使用 PHP 创建一个应用程序并涉及 MySQL。如果在请求过程中发生错误,我将如何“将查询分组在一起”,检查它是否会成功,然后对真实表进行实际影响。如果对表的实际更新失败,则恢复到更新之前的状
我正在尝试在 CentOs6 上使用 EasyBuild 编译 tensorflow,但 Bazel 捕获了错误的 binutils 目录,不幸的是,这是一个硬编码目录,以错误的汇编器结尾。 如果我尝
我正在尝试在 OS X Mavericks 上创建一个 AppleScript 库。我已将文件保存到 ~/Library/Script Libraries作为脚本包;它有一个正确的(据我所知;见下文)
我在 Eclipse Indigo 中开发 Android 项目已经有一年了,该 PC 已关闭更新且未安装任何新软件(故意避免 java 运行时或其他已安装软件出现任何不稳定)。 今天早上 Eclip
我正在开发一个简单的 React.JS 前端部分。 我本质上有一个用于历史数据的浏览 SPA。该设计有一堆过滤器,我需要一次填充一个过滤器,从逻辑层次结构中的顶部过滤器开始。 我做了类似的事情:
我已经搞乱了我的代码有一段时间了......现在当我运行它并在菜单中单击第二个选项时,当我输入超过 15 个字符的密码时,我收到此错误?谁能看到我做错了什么?这可能是一些我无法发现的小事谢谢 错误:
我有一个正在尝试构建的“单词”链接列表,我创建了一个名为“add_to_mem”的函数,它将下一个单词添加到链接列表中。我对代码进行了几次检查,发现他工作了两次 - 一次当链接列表为 NULL 时,一
我遇到了这个错误 *** Error in `./main': double free or corruption (out): 0x000000000095c8a0 *** getPieces()定
我的 jni 代码中有 malloc 问题。该代码旨在让我访问 java 中的 exiv2 库。我尝试使用 sun 和 openjdk 1.6.0 虚拟机以及 1.7.0(测试版)虚拟机破坏代码。 错
使用 visual studio 2013,我一直在尝试运行下面的代码,但是当变量 vertexPointer 达到数字 7172 时,我以某种方式收到“堆已损坏”异常。有时我会收到错误消息:“未加载
是否有一个约定俗成的errno代码表明指定的文件已损坏(它不符合应有的文件格式)? 我正在编写文件解析器,但不知道要返回的最合适的代码是什么。总是有 EINVAL,虽然我希望有比这更具体的东西,因为它
我的 date.php 是 -
这可能是个愚蠢的问题,但我在运行 *.jar 包时随机出现以下错误。有时程序运行完美,有时会中断,将其输出到控制台: *** Error in 'java': double free or corru
当运行脚本来筛选 抓取 网页时,我遇到“zend_mm_heap 已损坏”。请让我知道如何解决此问题。 最佳答案 我假设您正在使用带有 mod_php 的 Apache 来回答这个问题。 当 apac
我有将十六进制字符串转换为字节数组的函数, BYTE* HexStrToByteArray(std::wstring hex_str) { int len = hex_str.size()*0
请不要为了这个把我钉在十字架上。我决定使用 char* 可能更好,因为我打算构建的字符串的大小已知。我还知道,如果 timeinfo->tm_hour 返回的不是 2 位数字,那么事情就会变得很糟糕。
我有一个包含 vector 的类 (foo)。 如果我尝试像这样遍历 vector 中的元素: for(vector::iterator it = foo.getVector().begin();
每当我从我的 Remote 中 pull 出时,我都会收到以下关于压缩的错误。当我运行手动压缩时,我得到了相同的结果: $ git gc error: Could not read 381378312
我是一名优秀的程序员,十分优秀!