As the function is set of instruction stored in one contiguous block of memory.
And address of a function (entry point) is the address of the first instruction in the function. (from my knowledge)
And thus we can say that the address of function and the address of the first instruction in the function will be the same (In this case the first instruction is the initialization of a variable.).
But the program below contradicts the above line.
code:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char ** fun()
{
static char * z = (char*)"Merry Christmas :)";
return &z;
}
int main()
{
char ** ptr = NULL;
char ** (*fun_ptr)(); //declaration of pointer to the function
fun_ptr = &fun;
ptr = fun();
printf("\n %s \n Address of function = [%p]", *ptr, fun_ptr);
printf("\n Address of first variable created in fun() = [%p]", (void*)ptr);
cout<<endl;
return 0;
}
One output example is:
Merry Christmas :)
Address of function = [0x400816]
Address of first variable created in fun() = [0x600e10]
So, here the address of function and the address of first variable in function is not same. Why so?
I searched on google but can't come up with the exact required answer and being new to this language I exactly can't catch some of contents on net.
Other answers here already explain what a function pointer is and isn't. I'll specifically address why your test does not test what you thought it did.
This is not required (as other answers explain), but it's common, and it's usually a good intuition too.
Ok.
What you're printing here is the address of the variable. Not the address of the instruction that sets the variable.
These are not the same. In fact, they cannot be the same.
The address of the variable exists in a particular run of the function. If the function is called multiple times during the execution of the program, the variable can be at different addresses each time. If the function calls itself recursively, or more generally if the function calls another function that calls … that calls the original function, then each invocation of the function has its own variable, with its own address. The same goes in a multithreaded program if multiple threads happen to be invoking that function at a particular time.
In contrast, the address of the function is always the same. It exists regardless of whether the function is currently being called: after all the point of using a function pointer is usually to call the function. Calling the function multiple times won't change its address: when you call a function, you don't need to worry whether it's already being called.
Since the address of the function and the address of its first variable have contradictory properties, they cannot be the same.
(Note: it's possible to find system where this program could print the same two numbers, although you can easily go through a programming career without encountering one. There are Harvard architectures, where code and data are stored in different memories. On such machines, the number when you print a function pointer is an address in the code memory, and the number when you print a data pointer is an address in the data memory. The two numbers could be the same, but it would be a coincidence, and on another call to the same function the function pointer would be the same but the address of the variable could change.)
A function's address is only a symbolic way to hand this function around, like pass it in a call or such. Potentially, the value you get for the address of a function is not even a pointer to memory.
Functions' addresses are good for exactly two things:
to compare for equality
p==q
, andto dereference and call
(*p)()
Anything else you try to do is undefined, might or might not work, and is compiler's decision.
If I am not mistaken A program loads into two locations in memory. The first is the compiles executable including predefined functions and variables. This starts with the lowest memory that application occupies. With some modern Operating systems this is 0x00000 as the memory manager will translate these as needed. The second part of the code is the applications heap is where run-time allocated date such as pointers reside such any run time memory will have different location in memory
Why it would be so? A function pointer is a pointer that points to the function. It does not point to the first variable inside the function, anyway.
To elaborate, a function (or subroutine) is a collection of instructions (including variable definition and different statements/ operations) that performs a specific job, mostly multiple times, as required. It is not just a pointer to the elements present inside the function.
The variables, defined inside the function are not stored in the same memory area as that of the executable machine code. Based on the storage type, the variables which are present inside the function are located in some other part of the memory of the executing program.
When a program is built (compiled into an object file), different part of the program gets organized in a different manner.
Usually, the function (executable code), resides in a separate segment called code segment, usually a read-only memory location.
The compile time allocated variable, OTOH, are stored into the data segment.
The function local variables, usually are populated onto the stack memory, as and when needed.
So, there is no such relation that a function pointer will yield the address of the first variable present in the function, as seen in the source code.
In this regard, to quote the wiki article,
So, TL;DR, the address of a function is a memory location inside the code (text) segment where the executable instructions reside.