如何在代码中获取对象的类型?

发布于 2024-12-25 15:40:52 字数 217 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赤濁 2025-01-01 15:40:52

那是不可能的。 void * 指针只是指向内存的指针,仅此而已,没有附加任何信息。不可能执行您所要求的操作,您无法知道 malloc 有多少字节。

这就是为什么 qsort 函数>stdlib.h 库作为参数
每个数组元素的大小(以字节为单位)。如果你的建议是可能的,那么
qsort 不需要这样的参数。

也许你可以这样做:

...
Foo *f;
f = Allocate(f, sizeof(Foo));
...

void *Allocate(void *item, size_t size)
{
  return malloc(size);
}

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 to malloc.

That's why, the qsort function from stdlib.h library takes as a parameter
the size in bytes of each array element. If what you suggested was possible, then
qsort wouldn't need such a parameter.

Perhaps you could do something like this:

...
Foo *f;
f = Allocate(f, sizeof(Foo));
...

void *Allocate(void *item, size_t size)
{
  return malloc(size);
}
如梦亦如幻 2025-01-01 15:40:52

在 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.

傲性难收 2025-01-01 15:40:52

使用 void * 来获取该对象的 type 是不可能的。如果您可以在调用 Allocate() 之前计算 Foo 的大小,然后获取该大小并将其作为参数传递给 Allocate(),否则,您应该使 FooAllocate() 中可见以查找大小。

It is just not possible to get the type using a void * to that object. If you can calculate the size of Foo before calling Allocate(), then get the size and pass it as a parameter to Allocate(), else you should make Foo visible inside Allocate() to find the size.

浮光之海 2025-01-01 15:40:52

在 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.

月牙弯弯 2025-01-01 15:40:52

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 the Foo structure (assuming that it is) which knows how to allocate itself (the type is known here).

櫻之舞 2025-01-01 15:40:52
void *Allocate(void *item)
{
  return malloc(sizeof(f.GetType));
}

这是不可能的。 ISO C 语言不提供任何通过变量获取类型信息的工具。 Gcc 的扩展包含一个名为“typeof”的运算符,它可以从实际类型中获取类型信息。例如:

typeof (x[0](1))
int a; typeof (a) b; b = a;

但即使 typeof 也无法从 void 指针获取真实类型信息。下面的代码无法成功编译:

void *Allocate(typeof(*item) *item)
{
  return malloc(sizeof(*item));
}
void *Allocate(void *item)
{
  return malloc(sizeof(f.GetType));
}

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:

typeof (x[0](1))
int a; typeof (a) b; b = a;

but even though typeof also could not get the real type info from a void pointer. the code below could not be compiled succesfully:

void *Allocate(typeof(*item) *item)
{
  return malloc(sizeof(*item));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文