6

Obviously as you can tell, I am new to pointers. I came from Java with no pointers.

I still don't understand why we can dereference a function pointer? We can dereference an int pointer since we know an int is 4 bytes, but we can't dereference a void pointer since we don't know its length. For a function pointer, although we know the data type (length) of return value and parameters of a function, but we don't know how long the function will be in text, so how can we know the length of a function so that we can dereference it?

  • 1
    Perhaps [this](https://stackoverflow.com/questions/2795575/how-does-dereferencing-of-a-function-pointer-happen) question might be helpful. Explains what happens to function pointers when you do try to derefernce them the normal way (i.e. with `*`). – Bhargav Feb 27 '14 at 03:03
  • "deference" != "dereference" – William Pursell Feb 27 '14 at 03:09
  • 1
    Note that an `int` is not necessarily four bytes in C++. It's *at least* 16 bits, which is two bytes under normal circumstances, although 16-bit ints and normal don't go together too well nowadays anyway. – chris Feb 27 '14 at 03:21
  • The analogy with data pointers is a bit flawed. We can dereference a `Base*` even though it may in fact point to a `Derived` object of unknown size. – MSalters Feb 27 '14 at 09:57
  • I see that you are a bit confused about the length of the function pointer. See the below thread for better clarification http://stackoverflow.com/questions/3941793/what-is-guaranteed-about-the-size-of-a-function-pointer – Gurubaran Feb 27 '14 at 03:07
  • No, the question isn't about the length of a function _pointer_, but about the length of the function it points to. – MSalters Feb 27 '14 at 09:56

3 Answers3

20

A pointer is the equivalent of a piece of paper with a street address written on it in braille.

When you dereference it, you give it to a blind person who walks to the place. This blind person does not know if there is a pit, a house, a river or a mall there, unless you tell them: telling them is the type of the pointer.

Almost all addresses are the same length.

When you know a pointer is to an int, you are telling the blind person to expect an int at that address. So they walk there and interact with whatever is there as if it was an int.

When you know a pointer is a function pointer, and you dereference it and call it, the blind person doesn't need to know how big the function is: they just need to know where it starts. Functions are sort of like amusement park rides -- you get on them, then at some point they kick you off. Almost always they kick you off where you got on, lighter by whatever fell out of your pockets, and carrying a souvenir picture of your ride (the return value). (the exact details of what happens will depend on the calling convention)

Now, the signature of the function matters -- it tells you what the ride is expecting its passengers to bring aboard. And the return value type matters, because that tells you what shape of box to bring for the souvenir picture of your ride. But the exact size of the function matters not.

Naturally, we could go on about member function pointers, near and far pointers, references, and other more esoteric beasts.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
2

Good question. I had the same questions before. A function points is the same size as any other pointer. The only difference is that function pointers point to the text section of memory. Every time you make a method call, the computer dereferences the function pointer that is the name of the method.

Moonhead
  • 1,570
  • 8
  • 17
  • But we won't know its length, like void pointer, we know its address but we don't really know how many byte we will read.... – user3358627 Feb 27 '14 at 03:00
  • 1
    You don't need to know. All you need to know is that addresses are 4 Bytes long. We're not holding onto the entire data structure with pointers, but the address of this object. The function is pre-loaded by the compiler, and the only thing you need is to have the address so that your program knows which function to load at that particular place in your code. So technically you "DO" know how much memory it takes up. Hopefully that helps – Moonhead Feb 27 '14 at 03:01
  • Thanks for the clarity I think I understand it now – user3358627 Feb 27 '14 at 03:01
  • 1
    `A function points is the same size as any other pointer.` is plain wrong, there is no guarantee and void* is incompatible to any function pointer –  Feb 27 '14 at 03:13
  • @DieterLücking: To be precise: There's no such guarantee in ISO C++, although there is in both POSIX and Windows. – MSalters Feb 27 '14 at 09:55
0

In the abstract, "dereferencing" a pointer means converting it back to the thing that is pointed to. For concrete types the size of the thing is relevant, but that's determined by the type itself, not by the pointer. When you're calling a function the calling code certainly doesn't need to know how big the function is, so when you dereference a function pointer you don't need to know either.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622