【问题标题】:New declaration is containing garbage value and Heap corruption while using delete使用删除时,新声明包含垃圾值和堆损坏
【发布时间】:2017-01-10 19:42:45
【问题描述】:

我正在尝试读取 ANSI 格式的文件并将其转换为二进制文件。我正在声明两个动态内存分配,如下所示:char* binary_reverse = new char;char * binary = new char;

调试时我看到这个(二进制)包含太多垃圾值。为什么会这样?

我正在删除这些,例如:delete binary_reverse;删除二进制; 然而,在删除它给我的错误:

'ASCIItoBinary.exe':已加载 'D:\TryingBest\Reactice\ASCIItoBinary\Debug\ASCIItoBinary.exe',已加载符号。 “ASCIItoBinary.exe”:已加载“C:\Windows\SysWOW64\ntdll.dll”,找不到或打开 PDB 文件 “ASCIItoBinary.exe”:已加载“C:\Windows\SysWOW64\kernel32.dll”,找不到或打开 PDB 文件 “ASCIItoBinary.exe”:已加载“C:\Windows\SysWOW64\KernelBase.dll”,找不到或打开 PDB 文件 “ASCIItoBinary.exe”:已加载“C:\Windows\SysWOW64\msvcr100d.dll”,已加载符号。 HEAP [ASCIItoBinary.exe]:00241ED0 处的堆块在 00241EFD 处修改,请求大小为 25 Windows 已在 ASCIItoBinary.exe 中触发断点。

我是这样写代码的:

#include <cstring>

void AtoB(char * input)
{
    unsigned int ascii; //used to store ASCII number of a character
    unsigned int length = strlen(input);
    //cout << " ";
    for (int x = 0; x < length; x++) //repeat until the input is read
    {
        ascii = input[x];
        char* binary_reverse = new char;       //dynamic memory allocation
        char * binary = new char;
        //char binary[8];
        int y = 0;
        while (ascii != 1)
        {
            if (ascii % 2 == 0)    //if ascii is divisible by 2
            {
                binary_reverse[y] = '0';   //then put a zero
            }
            else if (ascii % 2 == 1)    //if it isnt divisible by 2
            {
                binary_reverse[y] = '1';   //then put a 1
            }
            ascii /= 2;    //find the quotient of ascii / 2
            y++;    //add 1 to y for next loop
        }
        if (ascii == 1)    //when ascii is 1, we have to add 1 to the beginning
        {
            binary_reverse[y] = '1';
            y++;
        }

        if (y < 8)  //add zeros to the end of string if not 8 characters (1 byte)
        {
            for (; y < 8; y++)  //add until binary_reverse[7] (8th element)
            {
                binary_reverse[y] = '0';
            }
        }

        for (int z = 0; z < 8; z++)  //our array is reversed. put the numbers in the rigth order (last comes first)
        {
            binary[z] = binary_reverse[7 - z];
        }
        //printf("the Binary is %s",binary);
        //cout << binary;   //display the 8 digit binary number

        delete binary_reverse;     //free the memory created by dynamic mem. allocation
        delete binary;
    }
}

我想要“二进制”中的确切二进制值。不是二进制值和垃圾?如何消除垃圾值?如何避免堆损坏?

【问题讨论】:

  • char* binary_reverse = new char; char * binary = new char; - 所以你分配 1 个字节用于存储
  • 题外话:节省一点代码:else if (ascii % 2 == 1) 可以是else。当处理单个二进制位时,它的值将是 1 或 0。如果不是一个,它必须是另一个。

标签: c++ windows visual-studio-2010


【解决方案1】:

问题是您使用new char 命令只分配了1 个字符。你想分配更多,使用new char[9]。由于您最多打印 8 位,因此您需要一个额外的字符作为空终止符。请务必在字符串末尾设置binary_reverse[y]=0

然后是delete[],而不是delete

但话虽如此,您应该改用std::stringstd::vector...

【讨论】:

  • 我试过这个。像 new char[8] 和 delete [] ,它仍然有同样的问题
  • 你发布的内容永远不会起作用,所以你应该发布一些有可能起作用的东西(即new char[8])。
  • 我已经更新了答案。您需要在字符串末尾添加一个空终止符。
  • 我做了这个 char* binary_reverse = new char[9];并删除 [] binary_reverse;。这也是我为二进制做的。堆仍然损坏。有人用 Windows 试过这个吗?对此的任何确切解决方案都会很棒。
【解决方案2】:

原来这里有很多错误,几乎所有这些都源于没有终止输出字符串,然后朝着错误的方向前进以寻找修复。

我将忽略

中的错误
char* binary_reverse = new char;

除了说 OP 需要更多存储空间。

char* binary_reverse = new char[8];

正确的方法是回到看起来像 OP 开始的临时分配,并添加一个额外的字节来包含字符串的空终止符。然后将该空间用作空终止符。

没有空终止符,你就没有字符串。你有一个二进制 blob。打印例程,所有 c 风格的字符串例程,都依赖于存在的终结符。没有它,他们不知道绳子在哪里结束,然后前往那边的狂野蓝色寻找它。经常会发生不好的事情。或者也许它没有。当你走出一个数组时会发生什么是未定义的。也许它会做你想要的。也许不是。没有办法确定。在这种情况下,从轨道上对站点进行核攻击甚至都行不通。

所以分配临时存储:

char binary_reverse[8]; // not using this one like a string so we don't need a terminator
char binary[9]; // printing this one. Need a terminator to know when to stop printing.

稍后,在binary_reverse被构造然后转移到binary之后,binary需要被终止以成为一个字符串,而不仅仅是另一个匿名二进制blob。

binary[8] = '\0';

现在可以打印了。

建议:

Visual Studio 有一个很棒的调试器。熟悉它。它会为您节省很多时间。

如果 OP 没有注释掉打印语句的可能性很大,那么昨晚有人会发现主要错误。最小化代码是好的,但是 OP 去除了 bug 的可见表现。

此代码可以大大简化。您知道您需要 8 位,因为您使用的是 ascii(好吧,实际上 ascii 是 7 位,但现在很少看到除了 8 位之外的任何东西)。将while (ascii != 1) 变成for (int count = 0; count &lt; 8; count++) 并测试字符中的所有8 位。稍后为您节省几个循环,因为现在您总是得到 8 位。

【讨论】:

  • 这两个问题仍然存在。有人尝试在 VS 中编写相同的代码并能够成功运行吗?
  • @TryingBest 总重写答案。
猜你喜欢
  • 2013-06-04
  • 2013-04-20
  • 2013-01-23
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多