- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想了解拥有一个 void 指针的真正需要,例如在下面的代码中,我使用强制转换能够以不同的方式使用相同的 ptr,那么如果可以的话,为什么真的有一个 void 指针类型转换?
int main()
{
int x = 0xAABBCCDD;
int * y = &x;
short * c = (short *)y;
char * d = (char*)y;
*c = 0;
printf("x is %x\n",x);//aabb0000
d +=2;
*d = 0;
printf("x is %x\n",x);//aa000000
return 0;
}
最佳答案
基本 C 不支持将任何指针类型转换为任何其他指针类型(即,没有 C 标准要求的任何扩展或行为的 C)。 2018 C 标准在第 6.3.2.3 条第 7 段中说:
A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned for the referenced type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer…
在那段话中,我们看到两个限制:
int *
到 short *
不太可能失败,因为 int
通常比 short
具有更严格的对齐方式.但是,base C 不支持反向转换。假设您用 short x[20];
定义了一个数组。或 char x[20];
.然后数组将根据需要对齐 short
或 char
, 但不一定是 int
的需要, 在这种情况下 (int *) x
的行为不会由 C 标准定义。int *
转换而来的指针。访问 short
.标准确实对某些指针转换做了一些额外的保证。其中之一是上述段落的延续:
… When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.
因此您可以使用从int *
转换而来的指针访问表示 int
的各个字节,并且您可以执行相同的操作来访问任何其他对象类型的字节。但是这种保证仅适用于访问具有字符类型的单个字节,而不是使用 short
。类型。
从上面我们知道在short * c = (short *)y;
之后在你的例子中,y
不一定指向 x
的任何部分它起源于——指针转换产生的值不能保证作为 short *
工作根本。但是,即使它确实指向了 x
所在的地方是,base C 不支持使用 c
访问这些字节,因为 6.5 7 说:
An object shall have its stored value accessed only by an lvalue expression that has one of the following types:
— a type compatible with the effective type of the object,
— a qualified version of a type compatible with the effective type of the object,
— a type that is the signed or unsigned type corresponding to the effective type of the object,
— a type that is the signed or unsigned type corresponding to a qualified version of the effective type of the object,
— an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), or
— a character type.
所以 *c = 0;
C 不支持您的示例,原因有二:c
不一定指向 x
的任何部分或任何有效地址,即使是,修改部分 int
的行为x
使用 short
C 标准未定义类型。它可能在您的 C 实现中有效,甚至可能受您的 C 实现支持,但它并不严格符合 C 代码。
C 标准提供了 void *
当特定类型不合适时使用的类型。 6.3.2.3 1 对指向 void
的指针做出类似的保证就像它对对象指针所做的那样:
A pointer to
void
may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer tovoid
and back again; the result shall compare equal to the original pointer.
void *
用于必须处理任意对象类型的例程,例如 qsort
. char *
可以达到这个目的,但最好有一个单独的类型,明确表示没有特定类型与之关联。例如,如果函数的参数是 char *p
, 该函数可能会无意中使用 *p
得到一个它不想要的角色。如果参数是void *p
,则函数必须在使用指针访问对象之前将指针转换为特定类型。因此,为“通用指针”设置一个特殊类型有助于避免错误,并向阅读代码的人表明意图。
关于c - 如果指针可以转换为任何类型(在 c 中),为什么要 void pointer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57980302/
作为作业的一部分,我正在尝试创建一个用户级线程库,如 pthreads。 为了处理线程之间的上下文切换,我使用了“swapcontext”函数。在使用它之前,我必须使用“makecontext”函数创
我是一名初级 C++ 程序员,我正在 Linux 机器上编程。 我遇到了这个错误: cannot convert ‘void* (Network::*)(void*)’ to ‘void* (*)(v
我知道,例如 void *(*myFuncName)(void*) 是一个函数指针,它接受并返回 void*。 这是一个有两个参数的指针吗?void 指针是该类型的另一个返回 void* 和 void
所以我被告知它们彼此几乎相同 void function1 (void(func)(int), int arg){ func(arg); } void function2 (void(*fun
我目前正在 GNU Radio 上开发一个 bloc,我想使用一个线程。该线程用于从 UDP 套接字获取数据,因此我可以在我的 GNU Radio 集团中使用它。 “一般工作”功能是执行所有信号和数据
我正在尝试在主函数中创建一个线程并通过我的线程调用另一个类的函数。 在 main.cpp 中: SocketHandler *callserver; pthread_t thread1; pthrea
我正在使用pthread 为我自己实现线程类。所以,我创建了 Thread 类如下: class Thread { public: Thread() { } virtual void*
我收到上述警告并理解它,但就是不知道如何解决它。我的代码在下面,但基本上我所做的是在结构中存储一个函数指针,并在我从 main.c 调用的另一个函数中初始化该结构。当我将代码与默认函数(即 free(
在我的 android 应用程序中,我在 doInBackground 中执行一些操作通过扩展 AsyncTask类(class)。 (我在这个类中执行任何 UI 都没用) 这是正确使用 AsyncT
我在 GNU 编译器集合中使用 C。所以我需要将函数指针传递给一个函数。现在有两种我想要处理的可接受的函数指针原型(prototype): void function(void); 和 void fu
我正在尝试使用“CameraManager”类创建一个新线程,但出现以下错误: cannot convert '*void(CameraManager:: * )(void*) to void*( *
我想构建一个可以隐藏线程创建的“IThread”类。子类实现“ThreadMain”方法并使其自动调用,如下所示: class IThread { public: void BeginThre
我不明白什么 void (**)(void *, const char *) /* ^^ why are there 2 asterisks here? 意思是,它是一个指向函数的指针,但我失败
我必须将“risposta”类型的参数“r”发送到函数 RispostaServer。编译器给我:invalid conversion void*(*)() to void*(*)(void*) 这是
所以我目前正在使用,或者至少正在尝试编写一个利用 this C pthread threadpool library. 的程序 值得注意的是 thpool.h 中的以下函数: int thpool_a
我正在尝试使用 void* 指针将不同的对象存储在一个全局表中。问题是如何取回 void* 对象。如果我有一个公共(public)基类,比如 Object ,我总是可以将 void* 指针存储为 Ob
我是一名 C 程序员(在 linux 上),但现在我有一个关于 C++ 的项目,并且有一个问题。 这里是示例代码 g_action.sa_sigaction = (void(*)(int,siginf
class Scoreget{ private: //some variables public: Scoreget(){ //
这个问题在这里已经有了答案: Is there a difference between foo(void) and foo() in C++ or C? (4 个答案) func() vs fun
我正在尝试使用 SDL 和 SDL_Mixer 为音频创建一个 C++ 应用程序,并且正在尝试遵循 this教程。但是,使用 SDL_Mixer 的 Mix_HookMusicFinished() 不
我是一名优秀的程序员,十分优秀!