简体   繁体   中英

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++.

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?). 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* br = new int();

Since the size of an int changes from implementation to implementation a portable way to do it is :

#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)". sizeof(int) is the number of chars (bytes) in an int. And multiplying both you get the number of bits in an 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. That means wasting more than 90% of that storage space. 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 :

A bitset stores bits (elements with only two possible values: 0 or 1, true or 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).

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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