How much memory does a C++ pointer use?

前端 未结 6 1841
南方客
南方客 2020-12-17 10:54

I\'m making the (possibly wrong) assumption that it is implementation and/or system dependent. Is there something like INT_MAX or CHAR_BIT that would tell me the size of a p

相关标签:
6条回答
  • 2020-12-17 11:20

    A pointer points into a place in memory, so it would be 32 bits on a 32-bit system, and 64 bits in 64-bit system.

    Pointer size is also irrelevant to the type it points at, and can be measured by sizeof(anyType*)

    UPD

    The way I answered this was suggested by the way the question was asked (which suggested a simple answer). Yes, I do agree that in cases like pointers to virtual method table, the size of the pointer will differ, and according to this article, it will vary on different platforms and even on different compilers within the same platform. In my case, for example (x64 ubuntu, GCC 4.6.3) it equals to 16 bytes.

    0 讨论(0)
  • 2020-12-17 11:24

    It is definitely system dependant. Usually a simple data pointer can be stored in a size_t variable. In C++11 there is SIZE_MAX macro which is the maximal value for size_t. In C++11 you could also use std::intptr_t.

    If you are taking member function pointers into consideration things get even more complicate. It depends among the other things if the class inherit from one or more parents, if it exposes virtual functions, and of course on the implementation.

    Here you can find a detailed article about member function pointers, along with some examples for few compilers.

    0 讨论(0)
  • 2020-12-17 11:24

    The answer for a simple case like int* is straightforward and is given on other answers.

    But keep in mind that pointers members of objects that use multiple inheritance may contain several (WORDs/DWORDs/QWORDs), in the worst case up to 5 (five).

    Good article about this: http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible

    0 讨论(0)
  • 2020-12-17 11:24

    Size of Pointer depends upon your Operating System (actually the number of bits of Operating System), try sizeof(type*) function.

    0 讨论(0)
  • 2020-12-17 11:26

    Would sizeof(int*) work? Or whatever you're meant to be checking?

    0 讨论(0)
  • 2020-12-17 11:37
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        printf(" %d",sizeof(int *));
        printf(" %d",sizeof(char *));
        printf(" %d",sizeof(float *));
        printf(" %d",sizeof(double *));
        getch();
    }
    
    0 讨论(0)
提交回复
热议问题