I got a problem with C programming.
Here is the Bitmap struct I defined:
typedef struct Bitmap {
uint *bits; uint n; // 规模为n的向量,记录被标记的次序,存储的是标记位在栈stack中的秩
uint *stack; uint top; // 容量为n的栈,记录被标记各位秩的栈,以及栈顶指针
void (*set)(struct Bitmap *map, uint i); // 将i位标记为1
bool (*test)(struct Bitmap *map, uint i); // 判断i位是1还是0
void (*clear)(struct Bitmap *map, uint i); // 将i位标记为0
void (*destroy)(struct Bitmap *map);
} Bitmap;
I an trying to write algorithm of finding all subset using Bitmap, this is the recursive procedure I wrote:
static Bitmap *bitmap;
void subset_using_bitmap(void *arr, uint cur, uint n, enum Types type) {
if (!bitmap)
bitmap = getAnBitmap(n);
if (cur == n) {
for (uint i = 0; i < cur; ++i)
if (bitmap->test(bitmap, i))
print(arr + i, type);
printf("\n");
return;
}
bitmap->set(bitmap, cur);
subset_using_bitmap(arr, cur + 1, n, type);
bitmap->clear(bitmap, cur);
subset_using_bitmap(arr, cur + 1, n, type);
}
And, below is the getAnBitmap method:
Bitmap *getAnBitmap(uint n) {
Bitmap map = {
.bits = (uint *)malloc(sizeof(uint) * n),
.n = n,
.stack = (uint *)malloc(sizeof(uint) * n),
.top = 0,
.set = set,
.test = test,
.clear = clear,
.destroy = destroy,
};
struct Bitmap *bitmap = ↦
return bitmap;
}
Finally, below is my problem: set() method:
static inline void set(Bitmap *map, uint i) {
map->stack[map->top] = i;
map->bits[i] = map->top;
++map->top;
}
when the program subset_using_bitmap
enter into bitmap->set(bitmap, cur)
secondly, the addresses of variables such as *bits, *stack
, and the contents of in program become illegal, for example, in my CLion, first *stack
is 0x382f08, then second *stack
become 0x1, obviously illegal.
Does the Inline keyword cause this? Hope expert can help us, Thanks very much!^_^