如何在代码中获取对象的类型?
C语言中如何判断对象的类型?我的目标是在 C 中创建一个链表容器。
假设我有一个采用 void 指针的函数:
...
Foo *f;
f = Allocate(f);
...
void *Allocate(void *item)
{
return malloc(sizeof(f.GetType));
}
如何使上述语法成为可能?
How do I figure out the type of an object in C? My goal with this is to create a linked-list container in C.
Let's assume I have a function that takes a void pointer:
...
Foo *f;
f = Allocate(f);
...
void *Allocate(void *item)
{
return malloc(sizeof(f.GetType));
}
How do I make the above syntax possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
那是不可能的。
void *
指针只是指向内存的指针,仅此而已,没有附加任何信息。不可能执行您所要求的操作,您无法知道malloc
有多少字节。这就是为什么
qsort
函数>stdlib.h 库作为参数每个数组元素的
大小
(以字节为单位)。如果你的建议是可能的,那么qsort
不需要这样的参数。也许你可以这样做:
That's not possible. A
void *
pointer, is just a pointer to the memory, nothing more, no more information is attached to it. It's impossible to do what you are asking, you can't know how many bytes tomalloc
.That's why, the
qsort
function fromstdlib.h
library takes as a parameterthe
size
in bytes of each array element. If what you suggested was possible, thenqsort
wouldn't need such a parameter.Perhaps you could do something like this:
在 C 中,没有跨平台的方法来找出 void* 的底层类型,因为类型信息不存储在对象中。在 C++ 中,已为对象添加了此功能。
当然,您可以自己为自己的对象实现它 - 例如 - 将每个对象的大小存储在对象本身中,但对于系统对象没有内置方法。
In C, there's no cross platform way to find out the underlying type of a void*, since the type information is not stored in the object. In C++ this feature has been added for objects.
You could of course implement it yourself for your own objects by - for example - storing the size of every object in the object itself, but for system objects there is no built in way.
使用
void *
来获取该对象的type
是不可能的。如果您可以在调用Allocate()
之前计算Foo
的大小,然后获取该大小并将其作为参数传递给Allocate()
,否则,您应该使Foo
在Allocate()
中可见以查找大小。It is just not possible to get the
type
using avoid *
to that object. If you can calculate the size ofFoo
before callingAllocate()
, then get the size and pass it as a parameter toAllocate()
, else you should makeFoo
visible insideAllocate()
to find the size.在 C 中执行此操作的唯一方法是创建您自己的类型系统,其中将 typeinfo 包含到您创建的类型中。这就是 Objective-C 中所做的事情。在 C 中,所有类型都只是一系列字节,当您分配内存时,您只是分配字节,C 编译器不关心您使用这些字节的用途。
The only way to do this in C is to create your own type system where you include typeinfo to the types that you create. That is what has been done for instance in Objective-C. In C, all types are just a series of bytes, when you allocate memory you are allocating just bytes, what you use those bytes for is of no concern to the C compiler.
C 的(有争议的)优点在于它不这样做。空指针只是一个可以指向任何东西的地址。您可以创建自己的对象系统(具有“类型”字段和“数据”字段的结构是一种简单的方法)。从您的实现中,也可以将
allocate
函数粘贴到Foo
结构中(假设是这样),该函数知道如何分配自身(类型在此处已知) )。The (arguable) beauty of C is that it doesn't do this. A void pointer is just an address that could point to anything. You could either create your own object system (structures with a "type" field and a "data" field is a simple way). From your implementation, it might also be possible to stick an
allocate
function into theFoo
structure (assuming that it is) which knows how to allocate itself (the type is known here).这是不可能的。 ISO C 语言不提供任何通过变量获取类型信息的工具。 Gcc 的扩展包含一个名为“typeof”的运算符,它可以从实际类型中获取类型信息。例如:
但即使 typeof 也无法从 void 指针获取真实类型信息。下面的代码无法成功编译:
It is impossible. ISO C language does't supply any facility to get the type info through variable. Gcc's extensions contains an operator called 'typeof', it could get the type info from a real type. such as:
but even though typeof also could not get the real type info from a void pointer. the code below could not be compiled succesfully: