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

Amit Upadhyay

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.

Sourav Ghosh

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the c program exit(0) equivalent function call in erlang?

From Dev

what does the function returns with in C program , in case if there is no return statement in the code

From Dev

C address of function parameter?

From Dev

What is error in this C program?

From Dev

C function to determine if IP address is multicast address

From Dev

C function to determine if IP address is multicast address

From Dev

c program prints same address for strings, unexpected output of c program

From Dev

c program prints same address for strings, unexpected output of c program

From Dev

Definition of a function in a C program

From Dev

what is Fixed address variable in C

From Dev

what is Fixed address variable in C

From Dev

Set the heap start address in C program?

From Dev

C program to print its own address space?

From Dev

C program address increment of array and array name

From Dev

C program address increment of array and array name

From Dev

Getting address of the function into the structure in C

From Dev

Function pointer to address (c++)

From Dev

C - address of matrix as an argument in function

From Dev

When embedding CLIPS into C Language, what function can used to modify the fact from C program

From Dev

When embedding CLIPS into C Language, what function can used to modify the fact from C program

From Dev

What is the address of buf (the local variable in the main function)?

From Dev

What are reasons for function call to address NULL?

From Dev

What is the function of "mailto:[email protected]"

From Dev

What is the entry address for a program loaded with SAM-BA?

From Dev

What is the difference between "Memory address" and "Program counter" registers?

From Dev

Identify what program queried specific domain/IP Address

From Dev

What is the last address of data segment in an assembly 8086 program?

From Dev

In a C# program, what is ThePreStub?

From Dev

What will be the output of following C program?

Related Related

  1. 1

    What is the c program exit(0) equivalent function call in erlang?

  2. 2

    what does the function returns with in C program , in case if there is no return statement in the code

  3. 3

    C address of function parameter?

  4. 4

    What is error in this C program?

  5. 5

    C function to determine if IP address is multicast address

  6. 6

    C function to determine if IP address is multicast address

  7. 7

    c program prints same address for strings, unexpected output of c program

  8. 8

    c program prints same address for strings, unexpected output of c program

  9. 9

    Definition of a function in a C program

  10. 10

    what is Fixed address variable in C

  11. 11

    what is Fixed address variable in C

  12. 12

    Set the heap start address in C program?

  13. 13

    C program to print its own address space?

  14. 14

    C program address increment of array and array name

  15. 15

    C program address increment of array and array name

  16. 16

    Getting address of the function into the structure in C

  17. 17

    Function pointer to address (c++)

  18. 18

    C - address of matrix as an argument in function

  19. 19

    When embedding CLIPS into C Language, what function can used to modify the fact from C program

  20. 20

    When embedding CLIPS into C Language, what function can used to modify the fact from C program

  21. 21

    What is the address of buf (the local variable in the main function)?

  22. 22

    What are reasons for function call to address NULL?

  23. 23

    What is the function of "mailto:[email protected]"

  24. 24

    What is the entry address for a program loaded with SAM-BA?

  25. 25

    What is the difference between "Memory address" and "Program counter" registers?

  26. 26

    Identify what program queried specific domain/IP Address

  27. 27

    What is the last address of data segment in an assembly 8086 program?

  28. 28

    In a C# program, what is ThePreStub?

  29. 29

    What will be the output of following C program?

HotTag

Archive