在 C 中使用 void * 代替重载?

发布于 2024-10-06 21:25:59 字数 712 浏览 3 评论 0原文

我的问题是,我在多线程应用程序中看到过这样的代码:

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

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

发布评论

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

评论(5

寄居人 2024-10-13 21:25:59

是的,void * 非常适合创建泛型函数。但是,一旦将指向特定数据类型的指针传递给采用 void * 的函数,您就会丢失所有类型信息。引导程序使其知道传入的类型的一种方法是使用 enum 类型的第二个参数,该参数可能具有 INT、FLOAT、DOUBLE 等值。

#include <stdio.h>

typedef enum inputTypes
{
    INT,
    DOUBLE,
    CHAR
} inType;

void myfunc(void*, inType);

int main(void)
{
    int    i = 42;
    double d = 3.14;
    char   c = 'a';

    myfunc(&i, INT);
    myfunc(&d, DOUBLE);
    myfunc(&c, CHAR);

    return 0;
}

void myfunc(void* param, inType type)
{  
    switch(type) {
       case INT:
           printf("you passed in int %d\n", *((int *)param));
           break;
       case DOUBLE:
           printf("you passed in double %lf\n", *((double *)param));
           break; 
       case CHAR: 
           printf("you passed in char %c\n", *((char *)param));
           break;
    }
}

输出

you passed in int 42
you passed in double 3.140000
you passed in char a

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 a void * 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 type enum which may have values such as INT, FLOAT, DOUBLE etc. etc.

#include <stdio.h>

typedef enum inputTypes
{
    INT,
    DOUBLE,
    CHAR
} inType;

void myfunc(void*, inType);

int main(void)
{
    int    i = 42;
    double d = 3.14;
    char   c = 'a';

    myfunc(&i, INT);
    myfunc(&d, DOUBLE);
    myfunc(&c, CHAR);

    return 0;
}

void myfunc(void* param, inType type)
{  
    switch(type) {
       case INT:
           printf("you passed in int %d\n", *((int *)param));
           break;
       case DOUBLE:
           printf("you passed in double %lf\n", *((double *)param));
           break; 
       case CHAR: 
           printf("you passed in char %c\n", *((char *)param));
           break;
    }
}

Output

you passed in int 42
you passed in double 3.140000
you passed in char a
花心好男孩 2024-10-13 21:25:59

是的,可以创建一个接受多种类型的函数,但这不是您的做法。

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 simple void * by itself cannot tell you what it is pointing to.

留蓝 2024-10-13 21:25:59

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.

咋地 2024-10-13 21:25:59

不起作用,因为 sizeof(param) 正在尝试获取指针的大小。

Doesn't work because sizeof(param) is trying to take the size of a pointer.

你曾走过我的故事 2024-10-13 21:25:59

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.

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