user18102815
user18102815

Reputation:

How to set pointer class to int value to pass into a function? How to set member of class to the first 8-bits of value in ByteWord?

I created 2 complex types BitsByte variable pointers and initially set them to nullptr. One will hold the uppers Byte of the word and the other will hold the lower Byte of the Word.

// File: BitsWord.h
class BitsWord
{
private:
int value;
int bits[16] = {};

    BitsByte* mLower{nullptr};
    BitsByte* mUpper{nullptr}; ...

I have a setValue method to set mUpper and mLower to the first two bytes of value that will convert the decimal number into the binary equivalent in an array named bits.

// BitsWord.cpp
void BitsWord::setValue(int value)
{ 
for(int i = 0; i < 16; i++)
    { bits[i] = static_cast<bool>(value & (1 << i)); }
reverse(begin(bits), end(bits));
}

I want to call the setValue method on mLower passing value. Debugger shows Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) on Xcode when I'm attempting to set mLower to the first 8-bits of value.

*mLower = value;     setValue(value);

Upvotes: 1

Views: 104

Answers (1)

ali_kh60
ali_kh60

Reputation: 1

You need to allocate memory to mLower before this line :

*mLower = value;

You should first create an instance of BitsByte, then assign the pointer mLower to point to that instance before calling setValue() function.

Upvotes: 0

Related Questions