n*******t 发帖数: 6 | 1 for example:
我有两个function pointer, 分别指向两个不同function。
complie的时候,这两个function pointer就得到function地址了吗?
可以用两个function pointer之差来indicate 系统stack 是upward 还是downward吗?
FLAG的一个面试官说不可以,因为function pointer是compile time 得到地址的。
stack 是runtime形成的。
然后把我往网上上的一个很popular的解法上引导。
但是网上所有关于这个解法的讨论都说,是因为compiler 优化造成compile time 的地
址和stack 无关。 这样,不是把gcc 的优化去掉,或者标成volatile不就可以了吗?
再问一个base class 和 相应 derived class的memory layout 能说明stack grow
direction 吗?
面试官也说不行。说,就是扯上virtual inheritance, 也是compile stage的。 |
s****a 发帖数: 794 | |
n*******t 发帖数: 6 | 3 可以用两个function pointer之差来indicate 系统stack 是upward 还是downward吗?
【在 n*******t 的大作中提到】 : for example: : 我有两个function pointer, 分别指向两个不同function。 : complie的时候,这两个function pointer就得到function地址了吗? : 可以用两个function pointer之差来indicate 系统stack 是upward 还是downward吗? : FLAG的一个面试官说不可以,因为function pointer是compile time 得到地址的。 : stack 是runtime形成的。 : 然后把我往网上上的一个很popular的解法上引导。 : 但是网上所有关于这个解法的讨论都说,是因为compiler 优化造成compile time 的地 : 址和stack 无关。 这样,不是把gcc 的优化去掉,或者标成volatile不就可以了吗? : 再问一个base class 和 相应 derived class的memory layout 能说明stack grow
|
g*********e 发帖数: 14401 | 4 当然是compile time得到的
当然runtime的时候load 到address space 里面,但他们的相对offset是不变的 |
k****i 发帖数: 128 | 5 有点迷惑,fp不是指向code section吗?stack是运行时call状态和fp有什么关系? |
n*******7 发帖数: 181 | 6 基本概念不清啊。function pointer是runtime 变量,可以赋函数地址的值。函数地址
是compile time决定。stack方向应该看stack pointer,和function无关。
:for example:
:我有两个function pointer, 分别指向两个不同function。 |
s**x 发帖数: 7506 | 7 我觉得应该是运行时得到的,编译时不可能完全定下来,这个应该是由loader 分配的
。象共享 library, 可以同时被多个程序共享,同一函数在不同程序的地址会完全不同
,这个不可能编译时确定。
当然,这个跟stack 没关系。 |
k****5 发帖数: 546 | 8 function pointer is just the memory address of the function image in code
section. Once loaded in memory, it is not changed.
there can be more than one instance of one function on the stack, either
direct or indirect recursion.
Allocate some heap and stack variables, check their addresses to get the
idea of memory layout of heap&stack.
class is about data layout, not about stack.
check any compiler book about runtime structure etc.
【在 n*******t 的大作中提到】 : for example: : 我有两个function pointer, 分别指向两个不同function。 : complie的时候,这两个function pointer就得到function地址了吗? : 可以用两个function pointer之差来indicate 系统stack 是upward 还是downward吗? : FLAG的一个面试官说不可以,因为function pointer是compile time 得到地址的。 : stack 是runtime形成的。 : 然后把我往网上上的一个很popular的解法上引导。 : 但是网上所有关于这个解法的讨论都说,是因为compiler 优化造成compile time 的地 : 址和stack 无关。 这样,不是把gcc 的优化去掉,或者标成volatile不就可以了吗? : 再问一个base class 和 相应 derived class的memory layout 能说明stack grow
|
k****5 发帖数: 546 | 9 once you take the address of one function, I don't think it should be
optimized out like inlining etc. But the compiler usually choose to not save
frame pointer on stack by default. But that does not mean there's no such
function in memory in running time.
【在 k****5 的大作中提到】 : function pointer is just the memory address of the function image in code : section. Once loaded in memory, it is not changed. : there can be more than one instance of one function on the stack, either : direct or indirect recursion. : Allocate some heap and stack variables, check their addresses to get the : idea of memory layout of heap&stack. : class is about data layout, not about stack. : check any compiler book about runtime structure etc.
|
n*******t 发帖数: 6 | 10 多谢大牛。
main() {
int a;
int b;
}
&b - &a >0 据说不能说明stack是up的,因为优化问题。
【在 k****5 的大作中提到】 : function pointer is just the memory address of the function image in code : section. Once loaded in memory, it is not changed. : there can be more than one instance of one function on the stack, either : direct or indirect recursion. : Allocate some heap and stack variables, check their addresses to get the : idea of memory layout of heap&stack. : class is about data layout, not about stack. : check any compiler book about runtime structure etc.
|