c++ - 使用删除时新声明包含垃圾值和堆损坏

标签 c++ windows visual-studio-2010

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

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

我正在删除这些:删除 binary_reverse;删除二进制文件; 但是,在删除期间它给我错误:

'ASCIItoBinary.exe': Loaded 'D:\TryingBest\Reactice\ASCIItoBinary\Debug\ASCIItoBinary.exe', Symbols loaded. 'ASCIItoBinary.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file 'ASCIItoBinary.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file 'ASCIItoBinary.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file 'ASCIItoBinary.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded. HEAP[ASCIItoBinary.exe]: Heap block at 00241ED0 modified at 00241EFD past requested size of 25 Windows has triggered a breakpoint in 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;
    }
}

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

最佳答案

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

然后 delete[] 而不是 delete

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

关于c++ - 使用删除时新声明包含垃圾值和堆损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41577052/

相关文章:

c# - Visual Studio 自动删除未被引用的方法的工具

c++ - 其他文件中的静态函数访问

c++ - 用 C/C++ 编写日志文件

windows - 确定推荐的系统要求

visual-studio-2010 - Visual Studio 2010 Professional 有序列图吗?

未强制执行 C++ 虚函数

c++ - qabstractitemmodel 数据在 qml 中没有改变

c++ - c/c++编译器如何知道错误在哪一行

windows - 为什么 `gcloud init` 没有显示所有文本?

c# - 使用 C# 将 Shell 集成到 Windows 中以获取特定文件类型