简体   繁体   English

错误生成十进制到二进制

[英]Error generating decimal to binary

I was writing a program to get binary representation of a number. 我正在编写一个程序以获取数字的二进制表示形式。 I wrote this code. 我写了这段代码。

#include <iostream>

using namespace std;
int main() {
    int n, count = 0;;
    int* br = new int();
    cin >> n;
    while (n>0) {
        if (n % 2)
            br[count] = 1;
        else
            br[count] = 0;
        n /= 2;
        count++;
    }
    cout << count << endl;
    for (int i = count - 1; i >= 0; i--) {
        cout << br[i];
    }
    return 0;
}

When I run the above program I get this error 当我运行上述程序时,出现此错误

Program received signal SIGTRAP, Trace/breakpoint trap.
0x00007ffe8995a2dc in ntdll!RtlpNtMakeTemporaryKey () from C:\Windows\SYSTEM32\ntdll.dll
Single stepping until exit from function ntdll!RtlpNtMakeTemporaryKey,
which has no line number information.
gdb: unknown target exception 0xc0000374 at 0x7ffe8995a31c

Program received signal ?, Unknown signal.
0x00007ffe8995a31c in ntdll!RtlpNtMakeTemporaryKey () from C:\Windows\SYSTEM32\ntdll.dll
Single stepping until exit from function ntdll!RtlpNtMakeTemporaryKey,
which has no line number information.
[Inferior 1 (process 6368) exited with code 0377]

What could be the possible reasons. 可能是什么原因。 I am new to C++. 我是C ++的新手。

The instruction 指令

int* br = new int();

allocates just one integer. 仅分配一个整数。 You should allocate at least as many bits as the data you want to convert (32? 64?). 您应至少分配与要转换的数据一样多的位(32?64?)。 I think you are better off using a static array, so 我认为您最好使用静态数组,因此

int br[64] ;

You should use an array of ints to hold the individual bits. 您应该使用一个整数数组来保存各个位。 Right now your code is creating a dynamic int : 现在,您的代码正在创建一个动态int:

int* br = new int();

Since the size of an int changes from implementation to implementation a portable way to do it is : 由于int的大小随实现的不同而变化,因此一种可移植的方法是:

#include<climits>
.....
int* br = new int[sizeof(int)*CHAR_BIT];

CHAR_BIT is a constant from climits with the "Number of bits in a char object (byte)". CHAR_BIT是climits中带有“ char对象中的位数(字节)”的常量。 sizeof(int) is the number of chars (bytes) in an int. sizeof(int)是int中的字符数(字节)。 And multiplying both you get the number of bits in an int. 然后将两者相乘即可得出int的位数。

Though you don't really need dynamic memory for that. 尽管您实际上并不需要动态内存。 It is far easier and more appropriate to use stack memory instead : 改而使用堆栈内存要容易得多,也更合适:

#include<climits>
.....
int br[sizeof(int)*CHAR_BIT];

The previous solutions have one common problem. 先前的解决方案存在一个普遍的问题。 All of them use a whole int to store a single bit. 它们全部使用一个完整的int来存储单个位。 That means wasting more than 90% of that storage space. 这意味着浪费了90%以上的存储空间。 An issue which is rather insignificant for such a simple example but which may become real on larger projects. 对于这样一个简单的示例而言,这个问题显得微不足道,但在大型项目中可能会变成现实。
std::bitset is an excellent way to improve that : std :: bitset是改善此问题的绝佳方法:

A bitset stores bits (elements with only two possible values: 0 or 1, true or false, ...). 一个位集存储位(元素只有两个可能的值:0或1,true或false,...)。

The class emulates an array of bool elements, but optimized for space allocation: generally, each element occupies only one bit (which, on most systems, is eight times less than the smallest elemental type: char). 该类模拟bool元素的数组,但针对空间分配进行了优化:通常,每个元素仅占用一位(在大多数系统上,此元素比最小元素类型char少八倍)。

#include<climits>
#include<bitset>
.....
bitset<sizeof(int)*CHAR_BIT> br;

std::bitset is so cleverly designed that you only need to change the declaration of br and it will work, no need to change the rest of the code. std :: bitset的设计如此巧妙,您只需要更改br的声明,它就可以工作,而无需更改其余代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM