0

I am trying to create a uint8_t array and try to change the first element of the array, then print it to the terminal as a string.

But, after I assign currBlock (changed block) to arr[0], cout gives an error. I tried to find the answer in StackOverflow but couldn't find a similar question. Can you help me with it?

Error: bitset::_M_copy_from_ptr

#include <iostream>
#include <sstream>
#include <bitset>
    
int main()
{
    uint8_t arr[3]{0};
    uint8_t currBlock{arr[0]};
    int flag{1};
    currBlock ^= (-flag ^ arr[0]) & (1UL << 3);
    cout << "Buffer is : " << bitset<24>(arr).to_string() << endl;
    arr[0] = currBlock;
    cout << "Buffer is : " << bitset<24>(arr).to_string() << endl;
    return 0;
}

I was expecting a print out of the uint8_t, but instead I get an error.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
sayonara
  • 13
  • 4
  • 3
    You should usually share the error message you received, as it can provide many clues as to what is happening. – Mikel F Jan 31 '23 at 00:20
  • 1
    `I got an error` First step is to read the error message. – eerorika Jan 31 '23 at 00:23
  • 1
    Is [this](https://godbolt.org/z/vaas9YeWG) the error? – Bob__ Jan 31 '23 at 00:24
  • 4
    Runtime error. Bitset is expecting a null-terminated string of 1s and 0s. You're not giving it one. Even before reading the error message you should [read the documentation](https://en.cppreference.com/w/cpp/utility/bitset/bitset) – user4581301 Jan 31 '23 at 00:25
  • There is no quick and easy way to do this, though I think there should be, so [here's some suggestions ho how to get this done right.](https://stackoverflow.com/q/708114/4581301) – user4581301 Jan 31 '23 at 01:17
  • I never realized before now that bitset doesn't expose iterators – Mooing Duck Jan 31 '23 at 03:20
  • @MikelF this was the error message. Error message is not that big – sayonara Jan 31 '23 at 07:31
  • I found a solution by iterating the array and assigning each element to new uint8_t and printing that one to terminal. It doesn't give error in this way, but it's weird – sayonara Jan 31 '23 at 07:37

0 Answers0