[UE 4.7.3] TBitArray GetAllocatedSize returns wrong value

Hello,

I have initialized TBitArray with 600 bits.
600 / 32 = 18,75 uints. This means, we need 76 bytes to store our array of bits.

Calling TBitArray:GetAllocatedSize returns 72 bytes, but this value is wrong.

Regards,
Chris

Hey ksvg07-

Could you provide more information on how you’re using your TBitArray? How is it setup and where are you calling GetAllocatedSize()? If possible, also include the code that defines and fills the array.

Cheers

Doug Wilson

To initialize array I am using:

Array.Init(false, 600);

Then some bits are set in code, does not matter which.
After that operation I want to get data in bytes.

The problem is that returned size is 72, not 76. This is because of improper calculation of bytes used in GetAllocatedSize routine.

	uint32 GetAllocatedSize( void ) const
	{
		return (MaxBits / NumBitsPerDWORD) * sizeof(uint32);
	}

I have done a workaround, by calculating size in bytes manually:

const uint32 GetArrayByteCount() const 
{ 
     /* 32 = bits per dword */
    return (Array.Num() + 32 - 1) / 32 * sizeof(uint32); 
}

Fixed code of GetAllocatedSize:

uint32 GetAllocatedSize( void ) const
{
	return (MaxBits + NumBitsPerDWORD - 1) / NumBitsPerDWORD * sizeof(uint32);
}

Hey ksvg07-

It seems that GetAllocatedSize() will round down rather which is what causes the math to equate incorrectly. I’ve bugged this behavior (UE-15908) however if you’ve found a working solution for the time being I would continue using that.

Cheers

Doug Wilson