Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the address of a function in a C++ program?

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.

like image 962
Amit Upadhyay Avatar asked Dec 25 '15 14:12

Amit Upadhyay


People also ask

Where is the address of a function stored in C?

Address of a function in C or C++ In C or C++, the variables are stored into memory, so we can get their memory addresses. Similarly, the functions also are stored into the memory, so they also have some addresses.

How do you get an address of a function?

We can get the address of a function by just writing the function's name without parentheses. Please refer function pointer in C for details. In C/C++, name of a function can be used to find address of function.

Where is the address of a function stored?

The address of a function is stored in a function pointer.

How do you print the address of a function?

Without using the separate pointer for print the address of the function, You can use the name of the function in the printf . printf("The address of the function is =%p\n",test); For printing the address in the hexa-decimal format you can use the %p .


2 Answers

So, here the address of function and the address of first variable in function is not same. Why so?

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,

Instead of referring to data values, a function pointer points to executable code within memory.

So, TL;DR, the address of a function is a memory location inside the code (text) segment where the executable instructions reside.

like image 167
Sourav Ghosh Avatar answered Sep 21 '22 04:09

Sourav Ghosh


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:

  1. to compare for equality p==q, and

  2. to dereference and call (*p)()

Anything else you try to do is undefined, might or might not work, and is compiler's decision.

like image 21
Aganju Avatar answered Sep 22 '22 04:09

Aganju