0

I'm working on a simple program which converts strings into binary code and back. I'm working on Ubuntu and wanted to compile the program on Windows with Visual Studio 2015. While the Linux build runs fine, on Windows it compiles, but crashes with something like

bitset<N> char

when calling following function:

bool bin2string(std::string *psBinaryString, std::string *psCharakterString)
{
psCharakterString->clear();
char cTempArray[8];
char cTemp;

for(unsigned int i = 0; i < psBinaryString->length(); i += 8)
{
    for(unsigned int j = 0; j < 8; ++j)
    {
        cTempArray[j] = psBinaryString->c_str()[j + i];
    }
    std::bitset<8> Bitset(cTempArray);
    cTemp = static_cast<char>(Bitset.to_ulong());
    psCharakterString->push_back(cTemp);
}
return true;
}

Now, my questions are, what is wrong with this code? Why does it work on Linux (gcc) and Windows (MinGW), but not on Windows with Visual Studio 2015?

My current workarround for this is:

#ifdef _WIN32

bool bin2string(std::string *psBinaryString, std::string     *psCharakterString)
{
psCharakterString->clear();
char cTempArray[8];
char cTemp;

for(size_t i = 0; i < psBinaryString->length(); i += 8)
{
    for(size_t j = 0; j < 8; ++j)
    {
        cTempArray[j] = psBinaryString->c_str()[j + i];
    }
    cTemp = static_cast<char>(strtol(cTempArray, 0, 2));
    psCharakterString->push_back(cTemp);
}
return true;
}

#else

bool bin2string(std::string *psBinaryString, std::string *psCharakterString)
{
psCharakterString->clear();
char cTempArray[8];
char cTemp;

for(unsigned int i = 0; i < psBinaryString->length(); i += 8)
{
    for(unsigned int j = 0; j < 8; ++j)
    {
        cTempArray[j] = psBinaryString->c_str()[j + i];
    }
    std::bitset<8> Bitset(cTempArray);
    cTemp = static_cast<char>(Bitset.to_ulong());
    psCharakterString->push_back(cTemp);
}
return true;
}

#endif

Which one of these two solutions is better?

HenrikS
  • 402
  • 3
  • 13
  • 2
    What do you mean by "crashes"? What actually happens? – Cody Gray - on strike Jan 03 '17 at 16:42
  • 4
    According to the documentation: http://en.cppreference.com/w/cpp/utility/bitset/bitset bitset's constructor expects a null-terminated string when the array length is not specified. You are lucky this works at all on linux. –  Jan 03 '17 at 16:45
  • @Frank The answer box is below. – NathanOliver Jan 03 '17 at 16:46
  • *Why does it work on Linux (gcc) and Windows (MinGW), but not on Windows with Visual Studio 2015?* -- You're lucky Visual Studio 2015 crashed, indicating you had a silent error in the Linux version. -- *My current workarround for this is:* -- No, don't do "workarounds". Whenever a compiler that is used by thousands of programmers around the world crashes with basically simple code, there is an error in your program, not a "bug" in the compiler. – PaulMcKenzie Jan 03 '17 at 17:31

1 Answers1

4

Your crash is caused by the fact that std::bitset's constructor expects either an array length, or a null terminated string.

(see the documentation at http://en.cppreference.com/w/cpp/utility/bitset/bitset)

So you should be using:

std::bitset<8> Bitset(cTempArray, 8);