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

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.


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.