[英]C queue implementation using void* - good or bad practice?
I have implemented a basic queue structure in C using void pointers. 我使用void指针在C中实现了一个基本的队列结构。 The procedure is as follows:
程序如下:
memcpy()
it to have a local copy. memcpy()
它有一个本地副本。 The struct itself looks like this: 结构本身看起来像这样:
struct queue
{
void* start; //pointer to the beginning of queue
void* end; //-||- to the end
size_t memsize; //size of allocated memory, in bytes
size_t varsize; //size of a single variable, in bytes
void* initial_pointer; //position of the start pointer before pop() operations
};
start and end are just void pointers that point to some location within the currently allocated memory block. start和end只是指向当前分配的内存块中某个位置的void指针。 If I push elements on the queue, I increment the end pointer by
varsize
. 如果我在队列上推送元素,我会通过
varsize
递增结束指针。 If I pop(), I just decrement the end pointer also by varsize
. 如果我pop(),我只是通过
varsize
减少结束指针。
I don't think I should post the functions code here, it's over 100 lines. 我不认为我应该在这里发布功能代码,它超过100行。
The question: is this considered a good or a bad practice? 问题:这被认为是好的还是坏的做法? Why (not)?
为什么不)?
Note: I'm aware that there are many other options for a queue in C. I'm just asking about the quality of this one. 注意:我知道C中的队列还有很多其他选项。我只是询问这个的质量。
EDIT: The implementation is available here: http:// 89.70.149.19 /stuff/queue.txt (remove the spaces) 编辑:可以在这里获得实现:http:// 89.70.149.19 /stuff/queue.txt(删除空格)
It's OK to use void *
if you don't know the type and size of the objects to be stored in the queue (in fact, the C standard library follows the same approach, see the memcpy()
and qsort()
functions for some examples). 如果您不知道要存储在队列中的对象的类型和大小,则可以使用
void *
(实际上,C标准库遵循相同的方法,请参阅memcpy()
和qsort()
函数例子)。 However, it would be better to use size_t
(or ssize_t
if you need a signed data type) for specifying the size of the elements stored in the queue. 但是,最好使用
size_t
(如果需要签名数据类型,则使用ssize_t
)来指定存储在队列中的元素的大小。
You really don't show us enough to be sure about your implementation. 你真的没有给我们足够的信息来确定你的实现。
void*
for the user data items is fine, you can't do much otherwise in C. void*
对于用户数据项是好的,否则在C中你做不了多少。
But I strongly suspect that you have an internal list element type that you use to manage the individual items, something like 但我强烈怀疑你有一个内部列表元素类型,用于管理单个项目,如
struct list_item {
struct list_item* next;
void* data;
};
If that is the case and your start
and end
pointers are pointing to such elements, you definitively should use your element type in the struct queue
declaration: 如果是这种情况,并且您的
start
和end
指针指向这些元素,那么您最终应该在struct queue
声明中使用您的元素类型:
struct queue
{
struct list_item* start; //pointer to the beginning of queue
struct list_item* end; //-||- to the end
size_t memsize; //size of allocated memory, in bytes
size_t varsize; //size of a single variable, in bytes
struct list_item* initial_pointer; //position of the start pointer before pop() operations
};
For this to work you don't even have to expose the definition of struct list_item
to the user of struct queue
. 为此,您甚至不必将
struct list_item
的定义暴露给struct queue
的用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.