Hi
I am about to create a bitmap that holds control of 512 bits, all 512 bits should be 0 to indicate that the bit is free when the program starts. Function occupyDataBlocks(int number) should find free bits put the position of the bit in the array int data_blocks[] and set the occupied bit to 1.
Under is some code that does some of the work: Except that char bit[512/8] is declared inside the function so the array will be declared when I call occupyDataBlocks something that makes the same output and a big fail in my prog, the programs return Not enough memory when I try to declare char bit as an global variable.
I need help to get the code to achieve this, and to set the bit to occupied. Please give me a coding hand, I have a understanding of the solutions but cant express it in C.
#include <stdio.h>
#include <string.h>
void occupyDataBlocks(int number)
{
int ab = number;
char bit[512/8];
int bitNum = 0;
int count;
int data_blocks[ab];
int b = 0;
for(bitNum = 0; bitNum < (sizeof(bit)/sizeof(char)); bitNum++) {
char x = bit[bitNum];
for(count = 0; x != 0; x >>= 1 ) {
if(!(x & 0)) {
data_blocks[b++] = count;
}
if(count == number) {
break;
}
count++;
}
if(count == number) {
break;
}
}
if(count == number) {
int a;
for(a = 0; a < 5; a++) {
printf("%d\n", data_blocks[a]);
}
} else {
printf("Not enough data blocks\n");
}
}
int main(void)
{
occupyDataBlocks(3);
occupyDataBlocks(3);
return 1;
}
#include <stdio.h>
#include <string.h>
int occupyDataBlocks(char bit, int number)
{
int ab = number;
int bitNum = 0;
int count;
int data_blocks[ab];
int b = 0;
for(bitNum = 0; bitNum < (sizeof(bit)/sizeof(char)); bitNum++) {
char x = bit[bitNum];
for(count = 0; x != 0; x >>= 1 ) {
if(!(x & 0)) {
data_blocks[b++] = count;
}
if(count == number) {
break;
}
count++;
}
if(count == number) {
break;
}
}
if(count == number) {
int a;
for(a = 0; a < 5; a++) {
printf("%d\n", data_blocks[a]);
}
} else {
printf("Not enough data blocks\n");
return 0;
}
return 1;
}
int main(void)
{
unsigned char bit[512/8];
/*
* I need 3 data blocks that is ready for me to use. Put the position to the free data block in array data_blocks[],
* where data_blocks[0] can be 100 (the first free data block), data_block[1] = 400 (second free datablock) etc.
*
*/
int data_blocks[3];
memcpy(data_blocks, occupyDataBlocks(bit, 3), sizeof(data_blocks));/* A try to copy the result of occypyDataBlocks to data_blocks*/
return 1;
}