I am going through a book to refresh my memory on data structures and c++ and I am reading through implementing a bitvector. In my Bitvector class, I have the following:
class BitVector
{
public:
Bitvector(int p_size)
{
m_array = 0;
m_size = 0;
Resize(p_size);
}
~Bitvector()
{
if(m_array !=0)
delete[] m_array;
m_array = 0;
}
void Resize(int p_size)
{
unsigned long int* newvector = 0;
if(p_size % 32 == 0)
p_size = p_size / 32;
else
p_size = (p_size /32) + 1;
newvector = new unsigned long int[p_size];
if(newvector == 0)
return;
int min;
if(p_size < m_size)
min = p_size;
else
min = m_size;
int index;
for(index = 0;index < min; index++)
newvector[index] = m_array[index];
m_size = p_size;
if(m_array != 0)
delete[] m_array;
m_array = newvector;
}
bool operator[](int p_index)
{
int cell = p_index / 32;
int bit = p_index % 32;
return (m_array[cell] & (1 << bit)) >> bit;
}
void Set(int p_index, bool p_value)
{
int cell = p_index / 32;
int bit = p_index % 32;
if(p_value == true)
m_array[cell] = (m_array[cell] | (1 << bit));
else
m_array[cell] = (m_array[cell] & (~(1 << bit)));
}
protected:
unsigned long int* m_array;
int m_size;
};
When I initialize the newvector pointer in the constructor, nothing is initialized so the array is in an undefined state correct? So, I'm using VS2010 and getting the following # when I step-into the code: 3452816845 for newvector. I understand that this is signaling that nothing is defined as of yet but is this value always the case for unsigned long int? Does this range ever vary (could this ever be 0)? I am curious because in the [] override, you bit-and this undefined identifier with the number you shifted into.
std::bitset<>for the purpose.