18

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 pointer in memory?

HorseloverFat
  • 3,290
  • 5
  • 22
  • 32
  • 1
    You should be careful with member function pointers, as they can have varying sizes, unlike regular ones. See http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible for more gory details. – Andrei Bârsan Jul 19 '12 at 13:05

6 Answers6

35

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.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • 3
    Wrong. On 64-bit OS, you can still have your program compiled in 32-bit mode. – Kan Li Jul 16 '12 at 10:02
  • I cout'ed `sizeof(int*)` and the answer was `4`. Does that mean 4 bytes? (somewhat interesting because this is a 64-bit machine). I am compiling with Visual C++. – HorseloverFat Jul 16 '12 at 10:06
  • 3
    @AdamSerafini Yes, it's 4 bytes = 32 bits. But you need to setup your project to compile it for a 64-bit, default it's 32-bit even if you use 64-bit OS. http://msdn.microsoft.com/en-us/library/9yb4317s(v=vs.80).aspx – tobi Jul 16 '12 at 10:09
  • Is the pointed type really irrelevant? What about virtual member function pointers? – Claudio Jul 16 '12 at 10:48
  • @BoBTFish,@Claudio, yes, my bad, I didn't think about function pointers. Updated the answer. – SingerOfTheFall Jul 16 '12 at 11:03
  • 1
    @BoBTFish In theory. In the past, also in practice, but today, such systems would be very rare. (Posix, for example, require that all pointers have the same size and representation.) Of course, if you mean pointers to members, those aren't really pointers, and point to member functions usually do require more memory than any other pointer type. – James Kanze Jul 16 '12 at 11:35
  • I should have been more specific, but yes, this was just a question about a pointer to an object (ie. not a function pointer). – HorseloverFat Jul 16 '12 at 22:53
5

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

Nathan White
  • 1,082
  • 7
  • 21
2

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.

Claudio
  • 1,658
  • 11
  • 18
2

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

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
  • How exactly can multiple inheritance affect the size of a pointer? – user207421 Jul 16 '12 at 11:07
  • It doesn't. As written, the answer is simply wrong. I suspect that the poster was thinking of pointers to members, which can have a distinctly different size than other pointers. (Of course, there's no requirement that all pointers have the same size, even in C, and I've worked on machines where `sizeof(int*) != sizeof(char*)`, or where pointers to functions had a different size than pointers to data.) – James Kanze Jul 16 '12 at 11:30
  • I currently cannot write a more detailed message. In a few hours I will update my post. – Kirill Kobelev Jul 16 '12 at 12:21
1

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

Tejendra
  • 1,874
  • 1
  • 20
  • 32
-1
#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();
}
  • used this program and its showing "2" as output for all printf statements. So, if I am not wrong, then a pointer takes 2 bytes.... – Sj StrykR Nov 15 '15 at 15:06
  • ...only on the specific and presumably quite old computer you used. There is no rule about the size, though on most contemporary computers, it will certainly be different from 2. – underscore_d Apr 11 '16 at 22:30