简体   繁体   English

函数签名指示返回类型为void *,但返回其他类型的指针

[英]Function signature indicates return type void*, but returns pointer of a different type

I'm confused about the following code, with the function signature indicating the function returns a void pointer. 我对以下代码感到困惑,函数签名指示函数返回一个空指针。 The type actually returned however, is BlockInfo*. 但是,实际返回的类型是BlockInfo *。 The function searches the first free memory block of size reqSize in a list. 该函数在列表中搜索大小为reqSize的第一个空闲存储块。 The whole program compiles and runs correctly. 整个程序可以编译并正确运行。

static void * searchFreeList(size_t reqSize) {
  BlockInfo* freeBlock;

  freeBlock = FREE_LIST_HEAD;
  while (freeBlock != NULL){
    if (SIZE(freeBlock->sizeAndTags) >= reqSize) {
      return freeBlock;
    } else {
      freeBlock = freeBlock->next;
      }
  }
  return NULL;

} }

My questions are: (1) Why is return freeBlock ,which returns type BlockInfo* a valid statement. 我的问题是:(1)为什么return freeBlock ,它返回BlockInfo *类型的有效语句。 (2) For a function that returns void pointer, when its return value is assigned to another pointer variable, eg: (2)对于返回void指针的函数,当其返回值分配给另一个指针变量时,例如:

int * ptr;  // Or double* ptr, etc.
ptr = searchFreeList(someSize);

Are these valid assignments? 这些有效的作业吗?

Thanks a lot! 非常感谢!

Why is return freeBlock,which returns type BlockInfo* a valid statement. 为什么返回freeBlock,它返回BlockInfo *类型的有效语句。

void * is a generic pointer type and is guaranteed to hold any object pointer type. void *是一种通用的指针类型,可以保证保留任何对象指针类型。 There is an implicit conversion between all object pointer types to void * so no cast is even needed in the return statement. 在所有对象指针类型到void *之间都有隐式转换,因此在return语句中甚至不需要强制转换。

int * ptr; int * ptr; // Or double* ptr, etc. ptr = searchFreeList(someSize); //或double * ptr,等等。ptr = searchFreeList(someSize); Are these valid assignments? 这些有效的作业吗?

Yes, they are valid assignments but dereferencing the pointer may be then undefined behavior if the underlying type was a different type. 是的,它们是有效的分配,但是如果基础类型是其他类型,则取消引用指针可能是未定义的行为。

For example: 例如:

char a = 42;
void *p = &a;
int *q = p;

*q;  // undefined behavior

In C, void * is a generic pointer type. 在C中, void *是通用指针类型。 It can point to any pointer type. 它可以指向任何指针类型。 So, 所以,

ptr = searchFreeList(someSize);  

is a valid assignment. 是有效的分配。

A void pointer can store address of any type and in this case it is storing address of type BlockInfo . void指针可以存储任何类型的地址,在这种情况下,它存储的是BlockInfo类型的地址。 So when a function returns a pointer of type BlockInfo it is totally fine because the prototype doesn't care for the type in this case. 因此,当函数返回类型为BlockInfo的指针时,这是完全可以的,因为在这种情况下原型不关心该类型。

The second part of your question where you ask whether 问题的第二部分,您在哪里询问

int *ptr = searchFreeList(someSize);

is valid. 已验证。 Yes

int *p = malloc(sizeof(int) *2);

is valid , here malloc() returns void * 是有效的,这里malloc()返回void *

The result will be that pointer of type int pointing to some struct and dereferencing it will be UB 结果将是int类型的指针指向某个结构并取消引用它的指针将是UB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM