C ++程序中函数的地址是什么? [英] What is the address of a function in a C++ program?
问题描述
函数的地址(入口点)是第一条指令的地址功能。 (据我所知)
因此我们可以说函数的地址和函数中第一条指令的地址是相同的(在这种情况下,指令是一个变量的初始化)。
但是下面的程序与上面的行相矛盾。
代码:
#include< iostream>
#include< stdio.h>
#include< string.h>
使用namespace std;
char ** fun()
{
static char * z =(char *)Merry Christmas :);
return& z;
}
int main()
{
char ** ptr = NULL;
char **(* fun_ptr)(); //指向函数的指针
fun_ptr =& fun;
ptr = fun();
printf(\ n%s \\\
函数地址= [%p],* ptr,fun_ptr);
printf(\\\
在fun()= [%p]中创建的第一个变量的地址,(void *)ptr);
cout<< endl;
返回0;
}
一个输出示例是:
圣诞快乐:)
函数地址= [0x400816]
在fun()= [0x600e10]中创建的第一个变量的地址
所以,函数地址和函数中第一个变量的地址不一样。为什么这样?
我在谷歌上搜索,但不能提出确切的必需答案,并且对这种语言不熟悉我完全无法捕捉到网上的一些内容。
所以,这里函数的地址和第一个变量的地址在功能不一样。为什么如此?
为什么会这样?函数指针是指向函数的指针。无论如何,它并不指向函数中的第一个变量。
详细说来,函数(或子例程)是一组指令(包括变量定义和不同的语句/操作),根据需要执行特定的工作,大多数时间。它不仅仅是指向函数内元素的指针。
函数中定义的变量不存储在相同的内存中区域与可执行机器代码的区域相同。根据存储类型,函数内部存在的变量位于执行程序内存的其他部分。
当一个程序被编译(编译成一个目标文件)时,程序的不同部分以不同的方式组织起来。
-
通常,函数(可执行代码)驻留在一个单独的段中,称为代码段,通常是只读内存位置。
编译时间已分配变量OTOH存储在数据段。 -
函数局部变量通常会根据需要填充到堆栈内存中。因此,没有这样的关系,函数指针将产生函数中存在的第一个变量的地址。
,如源代码所示。
不用引用数据值,函数指针指向内存中的可执行代码。
所以,TL; DR,函数的地址是可执行指令所在代码(文本)段内的内存位置。 >
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.
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.
这篇关于C ++程序中函数的地址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!