I am coding in C doing an assignment regarding block storage (for an OS class). My struct (which is typedef to block_store_t in another file) is
struct block_store{
unsigned char data[BLOCK_SIZE_BYTES][BLOCK_STORE_NUM_BLOCKS];
//block_size_bytes is 32, block_store_num_blocks is 512
};
I am putting all the data into a char array because chars are one byte long. To keep track of which blocks are in use, we use a bitmap which we have to store at block 127 of the block store data array. So, accessing it should be something like
bs->data[127]
or something similar to that. My code where I declare an initialize the block storage struct is like so:
block_store_t *block_store_create()
{
bitmap_t *bm = bitmap_create(BITMAP_SIZE_BITS);
block_store_t *bs = malloc(sizeof(block_store_t));
if (bs == NULL) return NULL;
for (int i = 0; i < (BITMAP_SIZE_BLOCKS); i++)
{
bitmap_set(bm, i + BITMAP_START_BLOCK);
}
//I've tried the ways two ways below, both give seg faults
//bs->data[BITMAP_START_BLOCK] = (unsigned char*)*bm;
//memcpy(bs.data[BITMAP_START_BLOCK], bm, BITMAP_SIZE_BYTES);
return bs;
}
Since the bitmap is of the type bitmap_t, how do I get the bitmap to be in block 127 of the data array, and how would I access it again? What if the bitmap is larger than the size of one block (in my case, the bitmap will be the size of two blocks, because 512 bits / 8 / 32 bytes per block)?
tl;dr How do I assign data that is not a char to a char array that is inside of a struct? I'm using char because it is one byte long.
bitmap_createdoes, what it returns or how it fails? How do we know whatbitmap_setdoes? How do we know thatBITMAP_SIZE_BLOCKSis longer than 144 (data + bitmap)? how do we know whatbitmap_teven is? - Mystbitmap_createallocates memory, you probably can't put the returned bitmap within theblock_store_t... you might want to create the bitmap some other way. Also, are you suebitmap_createtakes the bitmap size rather than theBITMAP_START_BLOCKlength? or maybe yourforloop should beBITMAP_SIZE_BITSlong instead? Are they the same value? And whyi + BITMAP_START_BLOCK? - Mystbitmap_createdoes allocate memory. I wonder what my professor wants us to do in that case, since we have to store that bitmap at block 127, but use his functions for the bitmap. - esb5415