在GNU C中,结果为13.因为使用了静态链接.
否则,如果使用动态链接,则结果为16.
#include <stdio.h>
int h(){
int x = 1;
int g(int z){
return z + x; <------------------ P
}
int f(int y){
int x = y + 1;
return g(x * y);
}
return f(3);
}
int main(){
int a = h();
printf("%d\n", a);
}
Run Code Online (Sandbox Code Playgroud)
在P点,激活记录是
z = 12
x = 4
y = 3
f和指向代码f的指针
g和代码g的指针
x = 1
h和指向代码h的指针
一个
main和指向代码main的指针
是对的吗?
但是,如果函数g返回,它将如何发生?
将删除激活g和激活变量z.
然后在堆栈框架中,查看孔.
洞真的出现了吗?
并且根据内联块,在函数h中,
变量x是最外层的块.(这意味着函数g'块嵌套在变量x的块中)下一个外部块是函数g,下一个函数f ...然后,函数f的静态链接指向函数g的帧指针吗?还是函数h的帧指针?那么函数g的静态链接怎么样?
是否可以使用嵌套函数/块编写可移植C代码?
我知道gcc只支持嵌套函数作为非标准扩展,而clang只支持块 - 但是有没有办法编写可以使用标准C和MACROS进行编译的代码?
如果不可能 - 最好的工作是什么?举个例子,如何实现一个带参数的以下类型的可移植版本?海湾合作委员会的简单例子:
int main(int argc, char*[] argv)
{
char reverse = 0;
int cmp_func(const void *a, const void *b)
{
const int* aa = (const int)a;
const int* bb = (const int)b;
return (reverse) ? aa - bb : bb - aa;
}
int list[8] = {1,2,3,4,5,20,100,200};
qsort(list, 8, sizeof(int), &cmp_func);
}
Run Code Online (Sandbox Code Playgroud)
可以使用Clang中的Blocks将类似示例放在一起.理想情况下,解决方案应该是线程安全的(因此避免使用全局变量).
编辑:为清楚起见,我们假设"标准"表示C99.以上是一个简单的例子.我所追求的是一种需要一些参数的C99方法.这里它只使用一个char作为布尔值,但我正在使用一个可以采用多个整数等的解决方案.看起来如果没有全局变量,这可能是不可能的.
编辑2:我意识到将void指针与函数指针一起传递使您可以执行可以使用嵌套函数完成的所有操作.感谢@Quuxplusone的建议qsort_r和qsort_s.我试图拼凑在便携式包装qsort_r和qsort_s.它需要一个比较器函数和一个用于存储状态的void指针,从而消除了对复杂排序算法的嵌套函数的依赖性 - 因此您可以使用GCC和Clang进行编译.
typedef struct
{
void *arg;
int (*compar)(const void …Run Code Online (Sandbox Code Playgroud) 我希望尽可能使用嵌套函数而不是Python中的方法或全局函数.所以我决定测试它们的性能,因为当你在另一个函数中定义一个函数时,在外部函数的每次调用中都会有内部函数定义的开销.
充其量我希望全局函数稍微快一点,但令人惊讶的是嵌套函数更快.有谁知道为什么?
这是我的代码:
from time import clock
def a(n):
return n + 1
def b1(loopcount):
return sum([a(n) for n in range(loopcount)])
def b2(loopcount):
def a(n):
return n + 1
return sum([a(n) for n in range(loopcount)])
powers = [5, 6, 7]
b1times = []
b2times = []
print " ", "".join(["{:^10d}".format(n) for n in powers])
for i in range(5):
for power in powers:
t = clock()
b1(10**power)
b1times.append(clock() - t)
for power in powers:
t = clock()
b2(10**power)
b2times.append(clock() - t) …Run Code Online (Sandbox Code Playgroud) 我最近碰到一个高尔夫球编码来到†中,我还以为写类型的函数问题int中main本身,它可以访问内部主要的变量.但是为了减少字符数,我想到了一个函数和一个变量.像这样的东西:
int i,f(){/*function code*/};
Run Code Online (Sandbox Code Playgroud)
我可以这样做吗?如果是/否,那么为什么?
† Code Golf是一种休闲节目,其中必须以最短的节目解决特定的挑战.减少源代码中的字符数是主要目标,可维护性和可读性并不重要.请在评论之前考虑此目标.
我正在尝试使用jquery的ajax函数来从我的ajax.php文件中获取一些信息.
码:
function ajaxIt(dataLine){
$.ajax({
type: "POST",
url: "ajax.php",
data: "ajax=true&"+dataLine,
success: function(msg){
console.log("[AjaxIt]: "+dataLine+" returned "+msg);
return msg;
}
});
}
if(ajaxIt("action=loggedIn")=="1"){
console.log("Logged In");
loggedIn=true;
initiate2();
}
Run Code Online (Sandbox Code Playgroud)
问题是我无法获得成功函数一直返回到ajaxIt函数.谁能解释一下我怎么做那样的事情?
谢谢.
拥有我认为应该是一个相对容易的问题来处理成为一个主要的痛苦......我正在尝试做:
a.b("param", function(data)
{
logger.debug("a.b(" + data.toString() + ")");
if (data.err == 0)
{
// No error, do stuff with data
}
else
{
// Error :( Redo the entire thing.
}
});
Run Code Online (Sandbox Code Playgroud)
我的方法是尝试:
var theWholeThing = function() {return a.b("param", function(data)
{
logger.debug("a.b(" + data.toString() + ")");
if (data.err == 0)
{
// No error, do stuff with data
}
else
{
// Error :( Redo the entire thing.
theWholeThing();
}
})};
Run Code Online (Sandbox Code Playgroud)
上面的问题是前者确实有效(除非发生错误时没有处理),后者根本不打印日志消息......就好像“theWholeThing()”调用没有像我认为的那样工作(再次调用整个事情)。
这里一定有什么微妙的错误,有什么提示吗?
谢谢!
我试图通过从私有范围内的函数中剥离参数来清理代码,如下所示:
Function complicatedFunction(x as Double, param1 as Double, param2 as Double)
...
End Function
Function mainActionHappensHere(L as Double, U as Double ...)
Function cleaner(x)
cleaner = complicatedFunction(x, L, U)
End Function
...
cleaner(x) 'Many calls to this function
...
End Function
Run Code Online (Sandbox Code Playgroud)
这可能吗?编译器抱怨"预期结束函数",因为我在结束外部函数之前开始执行函数.谷歌没有帮助:( PS我不能在mainActionHappensHere()之外定义cleaner(),因为那时正确的L和U不会被传递到它.
定义子函数是由父函数内部的父函数(作为嵌套函数)调用的吗?
例如,假设解决方案1:
Foo <- function(x) {
Baz <- function(y) {
#... do something
}
#... do something and call Baz, for instance
sapply(x, Baz)
}
Run Code Online (Sandbox Code Playgroud)
或者解决方案2:
Baz <- function(y) {
#... do something
}
Foo <- function(x) {
#... do something and call Baz, for instance
sapply(x, Baz)
}
Run Code Online (Sandbox Code Playgroud)
在解决方案1中,有一个额外的Baz运行定义过程Foo,所以我猜Foo在解决方案1中的许多调用会稍慢.这是真的吗?
我正在尝试使用 boost 的精神气解析器创建解析器。它正在解析包含三种类型值的字符串。常量、变量或函数。这些函数可以相互嵌套。测试字符串是f(a, b) = f(g(z, x), g(x, h(x)), c),其中a-e是常量,f-r是函数,s-z是变量。我成功创建了一个可以正确解析表达式的规则。当我将解析规则的函数更改为语法时出现了问题. 有几个错误我可以修复。我几乎掌握了解析表达式的语法,并将其转换为我创建的抽象语法树。但是,我收到了关于包含在 boost 库中的文件的这个错误,我无法弄清楚它来自哪里,因为我不理解编译器消息。我正在按照网站上的示例使用员工示例将数据从解析器放入结构:http : //www.boost.org/doc/libs/1_41_0/libs/spirit/example/qi/employee。 cp
主程序
#include "Parser.h"
#include "Term.h"
#include <boost/spirit/include/qi.hpp>
#include <string>
#include <iostream>
#include <list>
using std::string;
using std::cout;
using std::endl;
int main()
{
cout << "Unification Algorithm" << endl << endl;
string phrase = "f(a, b) = f(g(z, x), g(x, h(x)), c)";
string::const_iterator itr = phrase.begin();
string::const_iterator last = phrase.end();
cout << phrase << …Run Code Online (Sandbox Code Playgroud) 我正在上编程语言课.导师正在解释有关呼叫堆栈的问题.我怀疑导师无法正确解释.如果函数(func1)返回嵌套函数(比如func2并让func2使用func1中定义的变量).我们将返回的值保存在某个变量中,比如returnFunc.在func1中的return语句完成后,func1将退出.并且func1的堆栈帧应该从调用堆栈中弹出.我们现在在代码中的其他地方调用returnFunc.但是,returnFunc使用func1本地的变量,其堆栈帧不再存在于调用堆栈中.这是如何运作的.
示例python代码:
def func1():
a = 3;
def func2():
print(a)
return func2
returnedFunc = func1()
returnedFunc()
Run Code Online (Sandbox Code Playgroud)
这段代码正确打印3.然而我期待一些垃圾值,因为func1在调用堆栈上不再存在