void指针(void pointer)

本文详细解析了C/C++中void指针的概念及其使用规范,包括void指针可以指向任何数据类型,以及在ANSIC与GNU标准下对void指针的不同处理方式。同时,介绍了void关键字在限制函数返回值和参数方面的作用。

the meaning of void
void is "no type", void is "no type pointer", you can point to any data type

void pointer usage specification
1.void pointer can point to any data type, that is, any data type of pointer can assign to void pointer;
E.G.:
int *pint;
pvoid = pint; //but not pint = pvoid
if you want to assign pvoids to other types of pointer, you need to force type conversions such as:pint = (int*)pvoid

2.in the ANSA C standard,arthmetic operations on pvoid such as pvoid++ or pvoid+=1 are not allowed, but it is legal while in GNU, because GNU thinks void* is the same as char* by default. sizeof(*pvoid) = sizeof(char)

the role of void
2.1 limit the return of the function
2.2 limit the function paremeters
when the function does not need to return a value, you must use void to limit the return value type. for example: void func(int, int)
when the function is not allowed to accept parameters, you must use the void limit,for example: int func(void)

since void pointer can point to any type data, you can assign a void pointer to a any data type pointer,so you can also use the void pointer as a function paremeter, so that the function can accept any data type pointer as a parameter,
E.g:
void* memcpy(void*dest, const void* src, size_t len);

void的含义
void即“无类型”,void *则为“无类型指针”,可以指向任何数据类型。

void指针使用规范
①void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值。例如:
int *pint;
void *pvoid;
pvoid = pint; /* 不过不能 pint = pvoid; */
如果要将pvoid赋给其他类型指针,则需要强制类型转换如:pint = (int *)pvoid;


②在ANSI C标准中,不允许对void指针进行算术运算如pvoid++或pvoid+=1等,而在GNU中则允许,因为在缺省情况下,GNU认为void *与char *一样。sizeof( *pvoid )== sizeof( char ).

void的作用
①对函数返回的限定。
②对函数参数的限定。
当函数不需要返回值时,必须使用void限定。例如: void func(int, int);
当函数不允许接受参数时,必须使用void限定。例如: int func(void)。


由于void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值,因此还可以用void指针来作为函数形参,这样函数就可以接受任意数据类型的指针作为参数。例如:
void * memcpy( void *dest, const void *src, size_t len );
void * memset( void * buffer, int c, size_t num );

转载于:https://www.cnblogs.com/1915884031A-qqcom/p/7674124.html

### 关于Void指针的定义与特性 在C/C++编程语言中,`void*` 是一种特殊类型的指针,表示它可以指向任何数据类型。然而,由于其通用性,在使用过程中需要注意一些特定规则和约束条件[^1]。 #### Void指针的特点 - **无具体类型关联**:`void*` 指向的数据不具有具体的类型,因此无法直接对其进行解引用操作。 - **强制转换需求**:为了访问实际存储的内容,通常需要将其显式转换为目标类型。 ```cpp #include <iostream> using namespace std; int main() { int value = 42; void* ptr = &value; // Assigning address of an integer to a void pointer cout << "Value before cast: " << *((int*)ptr) << endl; // Explicitly casting back to int* double d_value = 3.14; ptr = &d_value; // Reassigning to point to a double cout << "Double Value after cast: " << *((double*)ptr) << endl; // Casting back to double* return 0; } ``` 这段代码展示了如何通过 `void*` 来间接操作不同类型的变量,并强调了每次读取前都需要正确地重新解释该指针所代表的实际类型的重要性[^1]。 #### 动态内存分配中的应用 当利用标准库函数如 `malloc()` 或者自定义实现动态内存管理机制时,返回的结果往往是一个 `void*` 类型的地址值。这是因为这些方法并不知道请求方希望获得何种确切形式的空间资源;它们仅仅负责提供足够的连续字节区域供后续进一步加工定制之用[^1]。 例如下面的例子说明了怎样借助 malloc 函数创建整数数组并初始化第一个元素: ```cpp #include <cstdlib> int main(){ int n=5; int *arr=(int *)malloc(n*sizeof(int)); if(!arr){ cerr<<"Memory allocation failed!"<<endl; exit(EXIT_FAILURE); } arr[0]=100; free(arr); return EXIT_SUCCESS; } ``` 此片段还包含了错误检测逻辑以及释放已分配空间的操作指南。 ### 常见注意事项及最佳实践建议 尽管灵活好用,但在涉及跨平台移植或者维护大型项目期间,滥用未加限制的空泛指针可能会引发难以追踪定位的问题。以下是几个推荐做法帮助规避潜在风险: - 总是在传递给其他子程序之前明确指定目标实体的确切类别; - 避免长期保留悬垂状态下的虚设链接对象以免造成不必要的混乱局面发生; - 对每一个新实例化的实例都应有相应的销毁措施配套执行以防止泄露现象累积恶化下去。 ### 结论 综上所述,虽然 `void*` 提供了一种强大而便捷的方式来处理未知或多种可能的数据结构,但是合理规划它的运用场景并且遵循良好的编码习惯对于构建稳健可靠的软件至关重要。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值