当我尝试显示时,如何防止堆栈溢出? [英] How do I prevent a stack overflow when I try to show it?

查看:139
本文介绍了当我尝试显示时,如何防止堆栈溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写ac#考试。



我的问题如下



< pre lang =c#> /// < 摘要 >
/ // 对于任何编码人员来说,这都是一个巨大的问题。你能纠正代码吗?
/// 描述它导致的问题?
/// < / summary >
/// < param < span class =code-summarycomment> name =count > < / param >
public void 递归( int count)
{
if (count < 20
{
递归(count ++);
}
}





我有什么方法可以为没有解决方案编写测试是否会导致考试失败?



我尝试过:



我尝试了一个属性,一个类,但是我尝试过的任何东西都没有像让考生知道他们知道++ var和var ++之间的区别

解决方案

如果仔细观察,很明显。



想想后缀操作实际上做了什么。这可能有所帮助:为什么x = ++ x + x ++给我错误的答案? [ ^ ]


1。设置 ManualResetEvent [ ^ ]



2.创建并启动新线程使用线程和线程| Microsoft Docs [ ^ ]:

a。调用测试方法

b。设置您在步骤1中创建的ManualResetEvent



3.(在原始主题中):调用 ManualResetEventSlim.Wait Method(System.Threading)| Microsoft Docs [ ^ ]具有合理的超时(只要您不使用int.MinValue调用,该秒就足够了。)



4.如果Wait返回false,请调用 Thread.Abort方法(System.Threading)| Microsoft Docs [ ^ ]并且测试失败。



如果它必须非常强大,我会考虑关闭测试到不同的应用程序域或进程。在主进程中有一个线程用完堆栈空间对我来说听起来有些冒险,毕竟它确实与其他所有东西共享地址空间。请记住,虽然此测试专门设计用于初始耗尽堆栈空间,但任何测试的任何答案都可能意外地完成。他们还可以调用命令来偶然格式化驱动器,但我假设您没有在自己的服务器上运行随机的人员代码。 :)


我建​​议你做两个小测试。

  • 添加一行打印计数 Recurse 方法开头的值。
然后

  • 使用预增量运算符( ++ cout )而不是后增量运算符修改递归调用。

I'm writing a c# exam.

I have the question as follows

/// <summary>
///     This is a huge problem for any coder.  Can you correct the code and
///     describe the issue it causes?
/// </summary>
/// <param name="count"></param>
public void Recursion(int count)
{
   if (count < 20)
   {
       Recursion(count++);
   }
}



Is there any way I can write a test for a solution without the risk of crashing the exam?

What I have tried:

I tried a property, a class, but nothing I tried can have the same affect as getting the examinee to show that they know the difference between ++var and var ++

解决方案

If you look at it closely, it's pretty obvious.

Think about what the postfix operation actually does. This may help: Why does x = ++x + x++ give me the wrong answer?[^]


1. Set up a ManualResetEvent [^]

2. Create and start a new Thread Using threads and threading | Microsoft Docs[^] that:
a. Calls the method under test
b. Sets the ManualResetEvent you created in step 1

3. (In the original thread): Call ManualResetEventSlim.Wait Method (System.Threading) | Microsoft Docs[^] with a reasonable timeout (a second should be enough as long as you do not call with int.MinValue).

4. If Wait returned false, call Thread.Abort Method (System.Threading) | Microsoft Docs[^] and fail the test.

If it had to be really robust, I would consider spinning the test off to a different appdomain or process. Having a thread run out of stack space in your main process sounds somewhat risky to me, after all it does share address space with everything else. And remember while this test is specifically designed to initial run out of stack space, any answer to any test could do this by accident. And they could also call commands to format the drive "by accident", but I assume you are not running random peoples code on your own server. :)


I suggest you two little tests.
  • Add a line printing the count value at the very begin of the Recurse method.
Then
  • Modify the recursive call, using the pre-increment operator (++cout) instead of the post-increment one.


这篇关于当我尝试显示时,如何防止堆栈溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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