Is the sizeof(some pointer) always equal to four?

前端 未结 17 1184
温柔的废话
温柔的废话 2020-11-22 11:49

For example: sizeof(char*) returns 4. As does int*, long long*, everything that I\'ve tried. Are there any exceptions to this?

相关标签:
17条回答
  • 2020-11-22 12:18

    In Win64 (Cygwin GCC 5.4), let's see the below example:

    First, test the following struct:

    struct list_node{
        int a;
        list_node* prev;
        list_node* next;
    };
    
    struct test_struc{
        char a, b;
    };
    

    The test code is below:

    std::cout<<"sizeof(int):            "<<sizeof(int)<<std::endl;
    std::cout<<"sizeof(int*):           "<<sizeof(int*)<<std::endl;
    std::cout<<std::endl;
    
    std::cout<<"sizeof(double):         "<<sizeof(double)<<std::endl;
    std::cout<<"sizeof(double*):        "<<sizeof(double*)<<std::endl;
    std::cout<<std::endl;
    
    std::cout<<"sizeof(list_node):      "<<sizeof(list_node)<<std::endl;
    std::cout<<"sizeof(list_node*):     "<<sizeof(list_node*)<<std::endl;
    std::cout<<std::endl;
    
    std::cout<<"sizeof(test_struc):     "<<sizeof(test_struc)<<std::endl;
    std::cout<<"sizeof(test_struc*):    "<<sizeof(test_struc*)<<std::endl;    
    

    The output is below:

    sizeof(int):            4
    sizeof(int*):           8
    
    sizeof(double):         8
    sizeof(double*):        8
    
    sizeof(list_node):      24
    sizeof(list_node*):     8
    
    sizeof(test_struc):     2
    sizeof(test_struc*):    8
    

    You can see that in 64-bit, sizeof(pointer) is 8.

    0 讨论(0)
  • 2020-11-22 12:21

    A pointer is just a container for an address. On a 32 bit machine, your address range is 32 bits, so a pointer will always be 4 bytes. On a 64 bit machine were you have an address range of 64 bits, a pointer will be 8 bytes.

    0 讨论(0)
  • 2020-11-22 12:23

    if you are compiling for a 64-bit machine, then it may be 8.

    0 讨论(0)
  • Technically speaking, the C standard only guarantees that sizeof(char) == 1, and the rest is up to the implementation. But on modern x86 architectures (e.g. Intel/AMD chips) it's fairly predictable.

    You've probably heard processors described as being 16-bit, 32-bit, 64-bit, etc. This usually means that the processor uses N-bits for integers. Since pointers store memory addresses, and memory addresses are integers, this effectively tells you how many bits are going to be used for pointers. sizeof is usually measured in bytes, so code compiled for 32-bit processors will report the size of pointers to be 4 (32 bits / 8 bits per byte), and code for 64-bit processors will report the size of pointers to be 8 (64 bits / 8 bits per byte). This is where the limitation of 4GB of RAM for 32-bit processors comes from -- if each memory address corresponds to a byte, to address more memory you need integers larger than 32-bits.

    0 讨论(0)
  • 2020-11-22 12:30

    The reason the size of your pointer is 4 bytes is because you are compiling for a 32-bit architecture. As FryGuy pointed out, on a 64-bit architecture you would see 8.

    0 讨论(0)
  • 2020-11-22 12:31

    Even on a plain x86 32 bit platform, you can get a variety of pointer sizes, try this out for an example:

    struct A {};
    
    struct B : virtual public A {};
    
    struct C {};
    
    struct D : public A, public C {};
    
    int main()
    {
        cout << "A:" << sizeof(void (A::*)()) << endl;
        cout << "B:" << sizeof(void (B::*)()) << endl;
        cout << "D:" << sizeof(void (D::*)()) << endl;
    }
    

    Under Visual C++ 2008, I get 4, 12 and 8 for the sizes of the pointers-to-member-function.

    Raymond Chen talked about this here.

    0 讨论(0)
提交回复
热议问题