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

前端 未结 17 1375
温柔的废话
温柔的废话 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):            "<

    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.

提交回复
热议问题