c++ - 关于void*的作用
PHPz
PHPz 2017-04-17 11:18:49
0
5
1101

void* 指针有什么用?(不是哪些情况用void,而是关于后面的疑问->)int 转v oid* 再转回 int* ,跟 int* 转 float* 再转回 int* 从结果来说不是一样的么?void* 作为"中间人"的功能,float* 也能做到,用void* 仅仅是为了“看起来”合乎逻辑?

PHPz
PHPz

学习是最好的投资!

reply all(5)
黄舟

void*Pointers can actually be used as generics. Imagine you want to exchange two variables in C. If they are two integers, it would be like:

void swap_int(int* lhs, int* rhs)
{
    int tmp = *lhs;
    *lhs = *rhs;
    *rhs = tmp;
}

If you want to exchange decimals, you need to write another

void swap_float(float* lhs, float* rhs)
{
    ;//
}

So since the bit patterns of the two variables are exchanged, this method can be abstracted:

void swap(void* lsh, void* rhs)
{
    ;//
}

Because you don’t know how many bits to exchange, you also need a parameter to specify the number of bits to exchange:

void swap(void* lsh, void* rhs, size_t size)
{
    ;//
}

With such a function, if you want to exchange two variables, you can:

swap(&ai, &bi, sizeof(int));
swap(&af, &bf, sizeof(float));

The questioner has modified the question, and I have added some points. Based on my knowledge, I am not sure whether the sentence after the questioner is correct

Use void* just to "look" logical

However, going back to the above example, if I use float* instead of void*, that is indeed possible, but why do I have to do a cast type conversion swap((float* )&ai, (float* )&bi, sizeof(int)) every time I call it? You put such a If the interface is used by another person, what will he think? Damn it, if you exchange two integers, you have to convert it to float* first. If you give it to a newbie and they don’t dare to use it, Damn, do you have any secrets here?

Indeed, void* and other pointers occupy the same size of memory space. The only difference from other pointers is that you cannot dereference void*. It seems that other types of pointers can be used void* Instead, just convert it to a pointer of the corresponding type before dereferencing and you're good to go!

Well, if that’s the case, then using void* is indeed to look logical. I can’t think of any situations where void* must be used!

迷茫

void * is mainly used for abstract data structures. Its specific uses include the following (not limited to):

  1. as variant, such as setsockopt;
  2. Hidden specific implementation (ADT): http://en.wikipedia.org/wiki/Abstract_data_type#Example:_implementation_of_the_stack_ADT
  3. As the userdata/opaque of the callback interface, the userdata/opaque is passed as a parameter to the callback processing function. You can think of this as a Context.
阿神

void* can actually be understood as a context, which can be used to implement C language object-oriented programming. I think using void* is not to look appropriate, but a convention. When people who program in C/C++ see void*, they will habitually think that this is private data, and only the party that defined it has the right to interpret it. ;This is also the basic consensus that we pass void* as userdata in the callback function.

伊谢尔伦

1. The sizeof of different types of pointers is the same. When allocating memory, if you don’t know whether the memory is int or float, you can let void* point to this memory and force transfer when using it.
2.int * is passed to the inside of the function as a parameter. It is not used inside the function for the time being. A warning will be issued during compilation. At this time, you can change it and it will not warn.
3. The number of bytes skipped by different types of pointers +1 is different.

迷茫

There is already a correct answer. It is recommended to read stanford cs 107b, which explains this aspect in detail.

In addition, so-called "modern languages" such as C++ and Java will generally help you provide generic mechanisms in the language itself. Type cast is considered bad in most cases.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template