问题描述
大家好,
我正在用C编写一个基于散列表的字典实现,而且它是
使用void指针进行数据存储,自然。但是,由于
之一我将使用的数据类型是unsigned long -unsigned
long,我认为它只是一个更好的主意unsigned longs
我插入到void指针插槽中以避免mallocs,释放和
不必要的引用。在我的机器上,这很好; void *是32
位。根据C
标准,所有机器都必须如此吗?我倾注了C89规格,并且无法找到任何关于
指针大小的内容;我认为它总是原生的单词大小,这将需要我想出一个不同的解决方案。
谢谢,
Rob Hoelz
Hello everyone,
I''m working on a hashtable-based dictionary implementation in C, and it
uses void pointers for data storage, naturally. However, since one of
the data types I will be using it for is unsigned long -unsigned
long, I figure it''d be a better idea to just cast the unsigned longs
I''m inserted into the void pointer slots to avoid mallocs, frees, and
unnecessary deferencing. On my machine, this is fine; void * are 32
bits. Is this necessarily the case on all machines as per the C
standard? I poured over the C89 specs and couldn''t find anything on
pointer size; I assume that it''s always native word size, which would
require me to think up a different solution.
Thanks,
Rob Hoelz
推荐答案
Nope(并且它不仅仅是一个标准的东西 - 真正的系统存在,其中
void *比32位窄,如果
不是系统,其中void *也大于32位,我会非常惊讶。
但是,你可以保证你可以将一个void *的对象表示形式放入一个带有sizeof(void *)
元素的unsigned char数组中。这可能足以满足您的需要。
-
Richard Heathfield< http://www.cpax.org.uk>
电邮:-www。+ rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的地方 - dmr 1999年7月29日
Nope (and it''s not just a Standard thing - real systems exist where a
void * is narrower than 32 bits, and I would be very surprised if there
were not systems with void * being wider than 32 bits, too.
You can, however, guarantee that you can fit the object representation
of a void * into an array of unsigned char having sizeof(void *)
elements. This may be adequate for your purposes.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
你不能使用void *和unsigned long的联合吗?这可能会使
界面当然有点麻烦。
- Richard
-
在一些字母表中应考虑需要多达32个字符
- 1963年的X3.4。
Can''t you use a union of void * and unsigned long? It may make the
interface a bit messier of course.
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
这篇关于最小空隙大小*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!