Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can we dereference a function pointer?

Tags:

c++

c

pointers

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?

like image 592
user3358627 Avatar asked Feb 27 '14 02:02

user3358627


People also ask

Why do we dereference a pointer?

Dereference a pointer is used because of the following reasons: It can be used to access or manipulate the data stored at the memory location, which is pointed by the pointer. Any operation applied to the dereferenced pointer will directly affect the value of the variable that it points to.

What happens when you dereference a pointer?

Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.

How do I dereference a pointer?

Pointers can be dereferenced by adding an asterisk * before a pointer.

What is dereferencing operator why it is used explain with example?

In Unix shell scripting and in utilities such as Makefiles, the dollar sign " $ " is the dereference operator, used to translate the name of a variable into its contents, and is notably absent when assigning to a variable.


2 Answers

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.

like image 90
Yakk - Adam Nevraumont Avatar answered Sep 20 '22 06:09

Yakk - Adam Nevraumont


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.

like image 41
KRUKUSA Avatar answered Sep 20 '22 06:09

KRUKUSA