在 C 中使用 void * 代替重载?
我的问题是,我在多线程应用程序中看到过这样的代码:
void Thread( void* pParams )
{
int *milliseconds = (int *)pParams;
Sleep(milliseconds);
printf("Finished after %d milliseconds", milliseconds); //or something like that
}
这极大地引起了我的兴趣,我知道 malloc
发送回一个 void 指针,您可以将其转换为您想要的内容,这是否意味着我可以创建一个可以接受任何数据类型的函数吗?
例如,我在没有测试的情况下编写的函数:
void myfunc( void* param )
{
switch(sizeof(param)) {
case 1:
char *foo = (char *)param; break;
case 2:
short *foo = (short *)param; break;
case 4:
int *foo = (int *)param; break;
}
}
myfunc(3.1415);
myfunc(0);
myfunc('a');
我可能完全错误,即使这确实有效,这是可怕的做法吗?谢谢。
My question here is I had seen code like this for a multithreading application:
void Thread( void* pParams )
{
int *milliseconds = (int *)pParams;
Sleep(milliseconds);
printf("Finished after %d milliseconds", milliseconds); //or something like that
}
This greatly took my interest, I knew malloc
sends back a void pointer and you can cast it to what you want, does this mean that I can create a function that can accept any data type?
For example a function I wrote without testing:
void myfunc( void* param )
{
switch(sizeof(param)) {
case 1:
char *foo = (char *)param; break;
case 2:
short *foo = (short *)param; break;
case 4:
int *foo = (int *)param; break;
}
}
myfunc(3.1415);
myfunc(0);
myfunc('a');
I may be completely wrong, even if this does work is it horrible practise? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,
void *
非常适合创建泛型函数。但是,一旦将指向特定数据类型的指针传递给采用void *
的函数,您就会丢失所有类型信息。引导程序使其知道传入的类型的一种方法是使用enum
类型的第二个参数,该参数可能具有 INT、FLOAT、DOUBLE 等值。输出
yes,
void *
is great for the creation of generic functions. However, once you pass a pointer to a certain datatype to a function that takes avoid *
you lose all type information. One way to steer your program so it knows which type you passed in is to have a 2nd parameter of typeenum
which may have values such as INT, FLOAT, DOUBLE etc. etc.Output
是的,可以创建一个接受多种类型的函数,但这不是您的做法。
在
myfunc
中,sizeof(param)
将始终相同:sizeof
在编译时确定,并且将是指针的大小。它适用于线程的原因是,通常编码人员在编译时就知道指针指向什么,而您所需要的只是一个简单的转换。如果您在编译时不知道
void *
指向什么,则必须在运行时以某种方式传递它。void *
的关键是:你只能将它们恢复到最初的状态,而你知道那是什么。一个简单的void *
本身无法告诉您它指向什么。Yes, it is possible to create a function that accepts multiple type, but that is not how you do it.
In your
myfunc
,sizeof(param)
will always be the same:sizeof
is determined at compile time, and will be the size of a pointer.The reason it works for threads, it generally it is known by the coder what the pointer points to, at compile time, and all you need is a simple cast. If you don't know at compile time what the
void *
points to, you must pass it at runtime somehow.The key with
void *
: You can only cast them back to whatever they were in the first place, and it is up to you to know what that is. A simplevoid *
by itself cannot tell you what it is pointing to.sizeof(param)
将始终返回指针的大小,因此如果您使用的是 64 位系统,则推测为 8。这与它所指向的内容无关。sizeof(param)
will always return the size of a pointer, so presumably 8 if you are on a 64-bit system. This has nothing to do with what it's pointing to.不起作用,因为
sizeof(param)
正在尝试获取指针的大小。Doesn't work because
sizeof(param)
is trying to take the size of a pointer.void*
的大小完全取决于系统并在编译时确定,而不取决于其中实际包含的内容。The size of a
void*
is completely dependent on the system and determined at compile time, not on what is actually contained within it.