Question
https://pastebin.com/XQbzNY6y #include #include #include bit_flags.h typedef struct bit_flags { int size; int capacity; int *bit; } Bit_flags; BIT_FLAGS bit_flags_init_number_of_bits(int number_of_bits) { Bit_flags* pBit_flags; if (number_of_bits
https://pastebin.com/XQbzNY6y
#include
#include
#include "bit_flags.h"
typedef struct bit_flags
{
int size;
int capacity;
int *bit;
} Bit_flags;
BIT_FLAGS bit_flags_init_number_of_bits(int number_of_bits)
{
Bit_flags* pBit_flags;
if (number_of_bits >= 1)
{
pBit_flags = (Bit_flags*)malloc(sizeof(Bit_flags));
if (pBit_flags != NULL)
{
pBit_flags->capacity = sizeof(int);
pBit_flags->size = number_of_bits;
pBit_flags->bit = (int*)malloc(sizeof(int));
if (pBit_flags->bit != NULL)
{
*pBit_flags->bit = 0;
}
else
{
pBit_flags = NULL;
}
}
}
else
{
pBit_flags = NULL;
}
return pBit_flags;
}
Status bit_flags_set_flag(BIT_FLAGS hBit_flags, int flag_position)
{
Status stat;
int bit_to_set = flag_position;
int* temp;
Bit_flags* phBit_flags = (Bit_flags*)hBit_flags;
while (flag_position >= phBit_flags->capacity)
{
phBit_flags->capacity *= 2;
temp = (int*)malloc(sizeof(int) * phBit_flags->capacity);
if (temp != NULL)
{
*temp = *(phBit_flags->bit);
free(phBit_flags->bit);
phBit_flags->bit = temp;
}
else
{
stat = FAILURE;
return stat;
}
}
flag_position /= phBit_flags->capacity;
phBit_flags->bit[flag_position] |= 1 capacity);
phBit_flags->size--;
stat = SUCCESS;
return stat;
}
Status bit_flags_unset_flag(BIT_FLAGS hBit_flags, int flag_position)
{
Status stat;
int bit_to_set = flag_position;
int* temp;
Bit_flags* phBit_flags = (Bit_flags*)hBit_flags;
while (flag_position >= phBit_flags->capacity)
{
phBit_flags->capacity *= 2;
temp = (int*)malloc(sizeof(int) * phBit_flags->capacity);
if (temp != NULL)
{
*temp = *(phBit_flags->bit);
free(phBit_flags->bit);
phBit_flags->bit = temp;
}
else
{
stat = FAILURE;
return stat;
}
}
flag_position /= phBit_flags->capacity;
phBit_flags->bit[flag_position] &= ~(1 capacity));
phBit_flags->size++;
stat = SUCCESS;
return stat;
}
int bit_flags_check_flag(BIT_FLAGS hBit_flags, int flag_position)
{
int bit, bit_set = flag_position;
Bit_flags* phBit_flags = (Bit_flags*)hBit_flags;
if (flag_position phBit_flags->capacity)
bit = -1;
else
{
flag_position /= phBit_flags->capacity;
if ((phBit_flags->bit[flag_position] >> bit_set) & 1)
{
bit = phBit_flags->bit[flag_position];
}
}
return bit;
}
int bit_flags_get_size(BIT_FLAGS hBit_flags)
{
Bit_flags* phBit_flags = (Bit_flags*)hBit_flags;
return phBit_flags->size;
}
int bit_flags_get_capacity(BIT_FLAGS hBit_flags)
{
Bit_flags* phBit_flags = (Bit_flags*)hBit_flags;
return phBit_flags->capacity;
}
void bit_flags_destroy(BIT_FLAGS* phBit_flags)
{
Bit_flags* pphBit_flags = (Bit_flags*)*phBit_flags;
free(pphBit_flags->bit);
free(pphBit_flags);
pphBit_flags = NULL;
printf("Bit flags object memory print destroyed! ");
}
Program crashes when calling check flag function on an unset bit: variable bit is used without being initialized Size should initially be equal to the number of bits requested by the user, and capacity should be equal to the maximum number of bits the data structure can hold without a resize; for example, with an initial size of 61, the structure would have a size of 61, 2 integers in the data array, and a capacity of 64. Currently the program allocates a single integer, regardless of flag position given
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started