首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c/c++ 中任意类型的指针的大小一定等于sizeof(void*)吗?解决办法

2012-03-23 
c/c++ 中任意类型的指针的大小一定等于sizeof(void*)吗?前几天看到一个帖子,说C++只规定了char 类型的大小

c/c++ 中任意类型的指针的大小一定等于sizeof(void*)吗?
前几天看到一个帖子,说C++只规定了char 类型的大小为一字节,而其它类型的大小是由具体的编译器决定的。

那么,任意一个类型的指针(除了成员指针)的大小是否一定与sizeof (void*) 相等呢

我在网上搜了一下,似乎不能保证它们的大小一定相等。

如果不相同,不妨设在某个编译器上sizeof( int* )=8; 而sizeof(void*)=4
那么对于下列语句:
int a;
void *vp = &a;  

vp保存的将是截断后的a的地址,而不是a的完整地址,而c/c++中而很多库函数的参数是void*类型的,如memset函数,如果不能保证任意一个类型的指针(除了成员指针)的大小与sizeof (void*) 相等,这些函数将不能正确执行。 
 
因此,我推测 c/c++中任意一个类型的指针(除了成员指针)的大小一定与sizeof (void*) 相等。
由于没有读过c/c++标准文档,不知道里面是怎么规定的。

[解决办法]

探讨

引用:
所有指针的sizeof都是一样大小,不管是否为成员指针还是什么别的什么类型的指针。


指向成员函数的指针大小不一定等于sizeof(void*)如:
C/C++ code

#include <iostream>
using namespace std;

class A
{
};

class B
{
};

class C:pu……

[解决办法]
探讨
前几天看到一个帖子,说C++只规定了char 类型的大小为一字节,而其它类型的大小是由具体的编译器决定的。

那么,任意一个类型的指针(除了成员指针)的大小是否一定与sizeof (void*) 相等呢?

我在网上搜了一下,似乎不能保证它们的大小一定相等。

如果不相同,不妨设在某个编译器上sizeof( int* )=8; 而sizeof(void*)=4
那么对于下列语句:
……

[解决办法]
这个应该查C语言标准的C99
6.3.2.3 Pointers
1 A pointer to void may be converted to or from a pointer to any incomplete or object
type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
。。。
6。。。
7。。。

[解决办法]
翻了一下C99,不巧,只有sizeof(char)==1,其它的没具体限制。
倒是有暗示可以不相等的:
7.18.1.4 Integer types capable of holding object pointers
1 The following type designates a signed integer type with the property that any valid
pointer to void can be converted to this type, then converted back to pointer to void,
and the result will compare equal to the original pointer:
intptr_t
The following type designates an unsigned integer type with the property that any valid
pointer to void can be converted to this type, then converted back to pointer to void,
and the result will compare equal to the original pointer:
uintptr_t
These types are optional.
那啥,成员指针大小各个编译器都不见得一样,像32位DMC++就都4字节,其它的8~20字节不等,取决于是否用到多继承/虚继承等等。。
找了一下,记得这篇文章里面应该有张表(我这里现在莫名其妙打不开……):
http://www.codeproject.com/KB/cpp/FastDelegate.aspx
[解决办法]
探讨
5.2.9 Static cast
10 A value of type pointer to object converted to “pointer to cv void” and back to the original pointer type will have its original value.

也就是如果T *p=100;那么 static_cast<T*>(static_cast<void*>(p)) == 100一定成立。

[解决办法]
给你贴上The C Programming Language 上的一段原话:
A.6.8 Pointers to Void
Any pointer to an object may be converted to type void * without loss of information. If the result is
converted back to the original pointer type, the original pointer is recovered. Unlike the pointer-topointer
conversions discussed in Par.A.6.6, which generally require an explicit cast, pointers may be
assigned to and from pointers of type void *, and may be compared with them.
void *类型的宽度是最大的,任何类型转换成void *都不会有信息丢失。
[解决办法]
关于类成员函数的指针的长度是和继承有关的,指向“类”的成员函数的指针不仅包含成员函数地址的信息,而且包含与类的属性有关的信息。在32位操作系统中,一般函数的指针的长度为4个字节(32位),无继承关系类的成员函数的指针长度为4字节(32位),普通继承关系的类成员函数的指针长度是8字节,有虚继承关系类成员函数的指针长度是12字节。



C/C++ code
#include "stdafx.h"#include <iostream>#include <typeinfo.h>using namespace std;class Test; //一个未定义的类。class Test2 //一个空类。{};class Test3 //一个有定义的类。{public:    //...    void (* memberfun)();    void Memberfun1( void (* f2)( ) ) { f2( ) ;} //成员函数1调用成员函数//2。    void Memberfun2( );//成员函数2。    //…};class Test4: virtual Test3 ,Test2 //一个有virtual继承的类(derivative class)。{public:    void Memberfun1( void (* f2)( ) ) { f2( ) ;} };class Test5: Test3,Test2 //一个继承类(derivative class)。{public:    void Memberfun1( void (* f2)( ) ) { f2( ) ;} };int main(){    std::cout <<"一般函数指针长度= "<< sizeof(void(*)()) << endl;    std::cout <<"-类的成员函数指针长度-"<<endl<<endl;    std::cout <<"Test3类成员函数指针长度="<< sizeof(void(Test3::*)())<<endl<<endl;    std::cout <<"Test5类成员函数指针长度="<<sizeof(void (Test5:: *)())<<endl;    std::cout <<"Test4类成员函数指针长度="<<sizeof(void (Test4:: *)())<<endl;    std::cout <<"Test类成员函数指针长度="<<sizeof(void(Test::*)()) <<endl;    return 0;}  一般函数指针长度= 4-类的成员函数指针长度-Test3类成员函数指针长度=4Test5类成员函数指针长度=8Test4类成员函数指针长度=12Test类成员函数指针长度=16Press any key to continue
[解决办法]
探讨
引用:

关于类成员函数的指针的长度是和继承有关的,指向“类”的成员函数的指针不仅包含成员函数地址的信息,而且包含与类的属性有关的信息。在32位操作系统中,一般函数的指针的长度为4个字节(32位),无继承关系类的成员函数的指针长度为4字节(32位),普通继承关系的类成员函数的指针长度是8字节,有虚继承关系类成员函数的指针长度是12字节。

C/C+……

[解决办法]
类成员指针不是指针,它实际上是一个句柄,当我们讲pointer type的时候,是不包括类成员指针的。

这个在C++标准里已经有所阐述。


此外,对于“指针即地址”这种谬论,以前已经批过了,不再重复。

而“64位整数偏移量”这种说法,更加说明搞不清楚抽象与实现。

这些似是而非的东西,跟“坦克即钢铁”、“人类即原子”没有什么两样。

这岂止撞枪口挂城门,简直就是剖腹自裁,内脏喂狗。
[解决办法]
http://www.adintr.com/mytranslate/fastdelegate.html

热点排行
Bad Request.