确定堆栈深度的标准方法 [英] standard way to determine depth in stack

查看:166
本文介绍了确定堆栈深度的标准方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在谷歌或新闻组中轻易找到这个


是否有标准功能/宏/您可以调用的任何内容并确定

C程序中的距离来自

main的C调用堆栈的深度。


有些事情:


int main()

{

int i = stackdepth();

f2()

}


这里我== 0.


void f2()

{

int i = stackdepth();

}


这里我== 1


等等。


我对非标准方式感兴趣(如果标准方式不足以支付b $ b),或任何疯狂的想法。

解决方案

所以* *********@gmail.com 写道:


我无法在google或者新闻组


是否有标准功能/ mac /你可以调用的任何内容并确定

C程序中距离来自

main的C调用堆栈的深度。



否 - 正如有些人喜欢指出的那样,可能没有堆叠!


>

我对非标准方式感兴趣(如果标准方式不符合
就足够了)或者任何疯狂的想法。



对于那些,你应该在论坛上询问你的编译器和/或操作系统。


-

Ian Collins。


softwaredoug写道:


有吗一个标准函数/宏/你可以调用的任何东西和

确定C程序中距离main的C / / $
调用堆栈中的距离。



没有标准方法。这取决于您的操作系统和

可能是您的编译器。在团队中询问您的操作系统中的编程。


-

问候,

Hallvard


所以********** @ gmail.com aécrit:


我无法在谷歌或新闻组中轻易找到这个


有吗一个标准函数/宏/你可以调用的任何东西确定

C程序中距离来自

main的C调用堆栈中的深度。


有些事情:


int main()

{

int i = stackdepth ();

f2()

}


这里我== 0.


void f2()

{

int i = stackdepth();

}


这里i == 1


等等。


我对非标准方式感兴趣(如果标准方式不符合

就足够了)或任何疯狂的想法。



你可以这样做:

1)在main函数中,获取第一个局部变量的地址:


void * stacktop;


int main(无效)

{

int dummy;


stacktop =( void *)& dummy;

......


}

2)

现在,你可以写


long long stackdepth(void)

{

int dummy;

long long top =(long long)stacktop;

long long bottom =(long long)& dummy;


long long result = top - 底部;

if(结果< 0)

结果= -result;

返回结果;

}


3)

请忽略所有愚蠢的评论,例如这不可携带。

这显然不能移植到任何机器上。它需要一个

堆栈,它假设一个地址适合很长的时间。


4)请忽略所有愚蠢的评论,例如这是未定义的行为

因为你不能把两个指针的区别指向不同的对象

同一个对象


玩得开心


jacob


I can''t see to easily find this on google or in a newsgroup

Is there a standard function/macro/whatever you can call and determine
the distance in a C program how deep one is in the C call stack from
main.

Something along the lines:

int main()
{
int i = stackdepth();
f2()
}

here i == 0.

void f2()
{
int i = stackdepth();
}

here i == 1

and so on.

I''m interested in nonstandard ways (if a standard way does not
suffice), or any crazy ideas.

解决方案

so**********@gmail.com wrote:

I can''t see to easily find this on google or in a newsgroup

Is there a standard function/macro/whatever you can call and determine
the distance in a C program how deep one is in the C call stack from
main.

No - as some people here enjoy pointing out, there might not be a stack!

>
I''m interested in nonstandard ways (if a standard way does not
suffice), or any crazy ideas.

For those, you should ask on a forum for your compiler and or OS.

--
Ian Collins.


softwaredoug writes:

Is there a standard function/macro/whatever you can call and
determine the distance in a C program how deep one is in the C
call stack from main.

There is no standard way. It depends on your operating system and
possibly your compiler. Ask in a group for programming in your OS.

--
Regards,
Hallvard


so**********@gmail.com a écrit :

I can''t see to easily find this on google or in a newsgroup

Is there a standard function/macro/whatever you can call and determine
the distance in a C program how deep one is in the C call stack from
main.

Something along the lines:

int main()
{
int i = stackdepth();
f2()
}

here i == 0.

void f2()
{
int i = stackdepth();
}

here i == 1

and so on.

I''m interested in nonstandard ways (if a standard way does not
suffice), or any crazy ideas.

You can do it as follows:
1) In the main function, get the address of the first local variable:

void *stacktop;

int main(void)
{
int dummy;

stacktop = (void *)&dummy;
......

}
2)
Now, you can write

long long stackdepth(void)
{
int dummy;
long long top = (long long)stacktop;
long long bottom = (long long)&dummy;

long long result = top - bottom;
if (result < 0)
result = -result;
return result;
}

3)
Please ignore all stupid remarks like "this is not portable".
This is obviously not portable to any machine. It requires a
stack, and it supposes that an address fits in a long long.

4) Please ignore all stupid remarks like " this is undefined behavior
since you can''t take the difference of two pointers that do not point to
the same object"

Have fun

jacob


这篇关于确定堆栈深度的标准方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆