Reputation: 25
I feel like I'm going crazy. I am trying to test logic for a bitmap I am implementing which is used to track frames in memory.
int main(){
unsigned char mask = 192; //1100 0000
unsigned char* bitmap;
bitmap[0] = 0x80;
bitmap[1] = 0xC8;
bitmap[2] = 0x2F;
bitmap[3] = 0x0;
int num_frames = 16;
int frame_size_in_bits = 2;
int desired_hole = 0;
int hole_counter = 0;
for(int i = 0; i < 4; ++i){
for(int j = 0; j < 4; ++j){
if((mask & bitmap[i]) == mask){
//std::cout << std::hex << int(mask) << " " << std::hex << int(bitmap[i]) << std::endl;
hole_counter++;
}
mask = mask/4;
}
mask = 192;
}
std::cout << "Num holes: " << hole_counter << std::endl; }
I am trying to check the number of times that 11 occurs in each byte of my bitmap. I have designated 11 as free for allocation. I check the byte with the mask, bitshift the mask to the right two times, and repeat. I have populated the bitmap with 3 "holes" where 11 occurs and satisfies my search.
However, when I run this code with the std::cout line commented out, I get that the number of holes is zero. I'm thinking "okay, my logic is off".
But when I enable the comment, I get my desired output.
Does anyone know why disabling/enabling this comment alters the value of hole_counter during code execution? This is leaving me scratching my head.
Thanks!
Upvotes: 1
Views: 51
Reputation: 610
You never allocated memory to "char* bitmap"
in C you can do it like
char *bitmap= NULL; /* declare a pointer, and initialize to NULL */
bitmap= malloc (5 * sizeof *bitmap);
in C++
unsigned char* bitmap= new unsigned char[4];
int main(){
unsigned char mask = 192; //1100 0000
unsigned char* bitmap= new unsigned char[4];
bitmap[0] = 0x80;
bitmap[1] = 0xC8;
bitmap[2] = 0x2F;
bitmap[3] = 0x0;
int num_frames = 16;
int frame_size_in_bits = 2;
int desired_hole = 0;
int hole_counter = 0;
for(int i = 0; i < 4; ++i){
for(int j = 0; j < 4; ++j){
if((mask & bitmap[i]) == mask){
//std::cout << std::hex << int(mask) << " " << std::hex << int(bitmap[i]) << std::endl;
hole_counter++;
}
mask = mask/4;
}
mask = 192;
}
std::cout << "Num holes: " << hole_counter << std::endl; }
Upvotes: 1