【发布时间】:2020-10-18 21:43:46
【问题描述】:
所以,我在重写我用 C 编写的 C++ 类时遇到了一些麻烦。
C++ 类有一些私有属性:
int grid_width;
int grid_height;
const int group_width = 2;
const int group_height = 4;
std::vector<int> buffer;
它是这样初始化的:
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);
}
它还带有一个清晰的功能,如下所示:
void grid::clear() {
// get_buffer_size returns elements in the buffer vector
for (int i = 0; i < get_buffer_size(); ++i) {
buffer[i] = 0x00;
}
}
现在,我尝试用 C 重写它看起来有点像这样:
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);
}
但由于某种原因,我的 C 代码中的元素数量总是 2? 有谁知道我在哪里搞砸了?
如果网格用 4 和 8 初始化,预期的缓冲区大小应该是 4,而不是 2。如果用 10 和 24 初始化,预期的大小将是 30,但在我的 C 示例中它仍然是 2 .
【问题讨论】:
-
sizeof(g->buffer)-- 这并不像你认为的那样。这不会给您分配的字节数。无论您使用的是C++还是C,都是这种情况。 -
如果您需要知道指向数组有多少元素,您需要将该值存储在某处。
std::vector在内部执行此操作,在 C 中您必须自己执行此操作(除非您使用具有很好的动态数组实现的库) -
一旦你解决了@PaulMcKenzie 提到的问题,你就会遇到其他问题。请注意,您只有一个
calloc,但有两个frees。你也永远不会初始化p_grid->buffer。我假设您打算让calloc为buffer分配空间,但这不是您使用它的方式。 -
另外,信不信由你,
C允许您按值返回struct,就像 C++ 一样。无需为grid_new函数返回grid *。声明一个本地grid,正确初始化,然后返回..grid grid_new(int grid_width, int grid_height)。以这种方式做事也可能会阻止@MilesBudnek 指出的错误
标签: c++ c arrays memory-management struct