如何处理溢出? [英] how to handle overflows?

查看:126
本文介绍了如何处理溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建我们自己的堆栈和堆溢出????

how to create our own stack and heap overflows????? and also what is the way for handling them?

推荐答案

处理"是什么意思?
A)在运行时检测?
B)一旦被检测到就做有意义的事情吗?

对于内存不足:
在Linux上,您可以使用 http://libsigsegv.sourceforge.net/ [ http://stackoverflow.com/questions/5785746/how-to-use-sigaltstack-in-signal-handler-程序 [ http://www.lehman.cuny.edu/cgi-bin /man-cgi?siglongjmp + 3 [ ^ ].

无论如何,我建议先捕获情况(信号处理程序),然后仅紧急保存一些关键数据.

干杯
Andi
What do you mean by "handle"?
A) Detect at run time?
B) Do something meaningful once it is detected?

For out-of-memory:
On linux you may use http://libsigsegv.sourceforge.net/[^] - also usable for stack overflow handling.

For stack-overflow:
Detect at runtime, see: http://stackoverflow.com/questions/5785746/how-to-use-sigaltstack-in-signal-handler-program[^].
Do something meaningful: see http://www.lehman.cuny.edu/cgi-bin/man-cgi?siglongjmp+3[^].

In any case, I suggest to first catch the situation (sig handler) and then to only panic-save some crucial data.

Cheers
Andi


您不能处理"堆栈溢出,只能避免它.通常是递归相互递归的结果,其中递归退出条件从未得到满足.请参阅:
http://en.wikipedia.org/wiki/Recursion [ http://en.wikipedia.org/wiki/Recursion_%28computer_science%29 [ ^ ],
http://en.wikipedia.org/wiki/Mutual_recursion [
You cannot "handle" stack overflow, you should only avoid it. It usually happens as a result of recursion or mutual recursion where recursion exit conditions are never met. Please see:
http://en.wikipedia.org/wiki/Recursion[^],
http://en.wikipedia.org/wiki/Recursion_%28computer_science%29[^],
http://en.wikipedia.org/wiki/Mutual_recursion[^].

With the heap overflow, the situation of much more complex. The problem is so difficult, that I would advise the following: if you develop in C, don''t worry about it, only worry about prevention of memory leaks and using too much memory. I can give you a more advanced answer only in brief, because this is a very difficult and complex topic: robust handling or stack overflow is possible only with some very advanced memory handling technologies (including but not limited by Garbage Collection), structured exception handling and languages which can effectively use it those technologies. I''m not saying it''s impossible with pure C, I would only say that doing in C along maybe does not worth bothering.

—SA


易于创建堆栈溢出:
Stack overflow is easy to create:
void MyFunction(){ MyFunction();}

会做得很好.

您不能溢出"堆-您所能做的就是继续分配内存,直到没有可用的内存并返回失败代码为止.

要处理堆,请检查malloc的返回-如果它为null,则操作失败.
您无法处理C中的堆栈溢出,因为它没有异常处理-应用程序将崩溃.


[edit]
进行此更改以显示回滚"按钮-OriginalGriff
[/edit]

Will do it very nicely.

You can''t "overflow" a heap - all you can do is keep allocating memory until is has none left and returns a failure code.

To handle the heap, check the return from malloc - if it is null, then the operation failed.
You can''t handle a stack overflow in C because it has no exception processing - the app will crash.


[edit]
This change made to show the Rollback button - OriginalGriff
[/edit]


这篇关于如何处理溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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