-1

I want to convert a bitfield to a string. Visual Studio 2008 gives an invalid null pointer exception.

Maybe it has something to do with the size of the array. it must be 8 but the output says it is 4, but why?

class Converter
{
public:
    string bitfieldToString (bool b_input[])
    {
        string c_conv;
        int i;
        for(i = 0; i < sizeof(b_input) ; i++)
        {
            if(b_input[i]=false){
                c_conv.append("0");
            }
            else if (b_input[i]=true){
                c_conv.append("1");
            }
            else c_conv = "Input is not a bitfield";break;
        }
        cout<<c_conv<<" "<< sizeof(b_input)<<endl;
        return (0);
    }
};
int main(void)
{
    Converter converter;
    bool b2[8] = {0,1,0,0,1,0,1,1};
    converter.bitfieldToString(b2);
    return (0);
}

Thank you! Now everything works as intended. and sorry for that dump question. I am new to C++.

Ramses
  • 15
  • 3
  • 1
    sizeof(bool[]) which should be sizeof(*bool) when I'm not completly wrong is 4 byte on a 32Bit Platform. – user743414 Feb 19 '15 at 12:03
  • 1
    The easiest way to do this is using one or more `std::bitset`'s and the [`std::bitset::to_string()`](http://en.cppreference.com/w/cpp/utility/bitset/to_string) function. – πάντα ῥεῖ Feb 19 '15 at 12:03
  • `sizeof(b_input)` This doesn't work. Pass the array size as a parameter or use `std::vector` – Neil Kirk Feb 19 '15 at 12:04
  • possible duplicate of [C++ string to binary code / binary code to string](http://stackoverflow.com/questions/18937892/c-string-to-binary-code-binary-code-to-string) – Jonathan Mee Feb 19 '15 at 12:08
  • I do -1 since this doesn't contain any problems that are bitfield related so missleading! – dhein Aug 14 '15 at 09:38

2 Answers2

2

There is a lot wrong in your code.

First of all, the null pointer exception comes from return (0); at the end of the bitfieldToString fuction. You have defined it to return a string; when you return 0 instead, C++ thinks 0 is a char* pointer and will try to convert it - a NULLpointer - into a string, which will crash. You should probably be returning c_conv instead.

Second, sizeof(b_input) will always be the size of a bool pointer. On a 32-bit system it will be 4, on a 64-bit system 8. You cannot get the length of an array passed as argument with sizeof; you will need to add a length parameter to your function.

Third, inside your for loop, you are assigning to b_input[i] instead of comparing the values. Use ==, not =.

Fourth, in the last else branch, you are missing braces. Essentially, the break will always break out of the loop after the very first iteration.

jlahd
  • 6,257
  • 1
  • 15
  • 21
2

The exception is because you return (0);. That's interpreted as a null pointer, used to initialise a std::string with a constructor which requires a valid pointer to a C-style string - not a null pointer.

That should be return c_conv;

The size mismatch is because b_input isn't an array. As a function parameter, bool b_input[] is a pointer. You can't pass an array to a function by value; and there's no way to determine the array size from the pointer alone. So sizeof(b_input) gives you the size of a pointer, not the array, and everything goes wrong.

There are a few options. You could pass the size as a second parameter; but that's error-prone. You could infer the size as a template argument, by taking the array by reference:

template <size_t size>
string bitfieldToString (bool (&b_input)[size])

You could use std::array or std::vector, which have handy size() member functions. (But be careful with vector<bool>, since it's a special case that doesn't always behave quite like a standard container.) Or you could use std::bitset, which has a handy to_string function that does exactly what you want.

Finally, enable your compiler's warnings - it should tell you not to use = where you mean ==. And there's not much point checking for the case of a boolean being neither true nor false. You can reduce the whole loop body to

c_conv.append(b_input[i] ? '1' : '0');
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644