So, I am having some trouble rewriting a C++ class I made in C.
The C++ class has some private attributes:
int grid_width;
int grid_height;
const int group_width = 2;
const int group_height = 4;
std::vector<int> buffer;
It is initialized like so:
grid::grid(int width, int height) {
this->grid_width = width;
this->grid_height = height;
buffer.resize(this->grid_width / this->group_width * this->grid_height / this->group_height, 0);
}
It also comes with a clear function like so:
void grid::clear() {
// get_buffer_size returns elements in the buffer vector
for (int i = 0; i < get_buffer_size(); ++i) {
buffer[i] = 0x00;
}
}
Now, my attempt to rewrite this in C looks somewhat like this:
typedef struct
{
int width;
int height;
int *buffer;
} grid;
grid *grid_new(int grid_width, int grid_height)
{
if ((grid_width % 2 != 0) || (grid_height % 4 != 0))
return NULL;
int group_height = 4;
int group_width = 2;
grid *p_grid = calloc(grid_width / group_width * grid_height / group_height, sizeof(int));
p_grid->width = grid_width;
p_grid->height = grid_height;
return p_grid;
}
void grid_free(grid *p_grid)
{
free(p_grid->buffer);
free(p_grid);
}
void grid_clear(grid *g)
{
// ToDo: Iterate over all elements in the buffer
int elements = sizeof(g->buffer) / sizeof(int);
printf("Elements: %i", elements);
}
But for some reason, the amount of elements in my C code is always 2? Does anyone know where I am messing up?
If the grid is initialized with 4 and 8, the expected buffer size should be 4, not 2. If it would be initialized with 10 and 24, the expected size would be 30, but it still remains 2 in my C example.