我有以下代码示例来说明我的观点.当我在Vista上的IE8中加载它时,我收到错误"Stack Overfow at line:16"
如果我使用顶级函数(在testClass对象之外)递归,我可以在没有堆栈溢出的情况下递归数百万次.
为什么会这样?最后我只是实现了一个函数Que而不是使用递归,但对我来说没有意义,我想了解原因.
- 代码 -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
<html>
<head>
<title>Recusion Test</title>
<body>
</body>
<script type="text/javascript">
function testClass() {
this.x = 15;
this.recurse = function() {
this.x--;
this.recurse();
}
}
var wtf = new testClass();
wtf.recurse();
alert('done');
</script>
</head>
</html>
Run Code Online (Sandbox Code Playgroud) 嗯,我遇到了一个奇怪的问题。当我创建类的新实例时,我得到StackOverflowExcepion :)
这是代码:
public partial class PlayerChooser : Window
{
public PlayerChooser()
{
InitializeComponent();
textBoxPlayer1Name.Visibility = Visibility.Hidden;
textBoxPlayer2Name.Visibility = Visibility.Hidden;
textBoxPlayer3Name.Visibility = Visibility.Hidden;
textBoxPlayer4Name.Visibility = Visibility.Hidden;
}
public static String player1Name;
public static String player2Name;
public static String player3Name;
public static String player4Name;
...
PlayerChooser.player1Name = textBoxPlayer1Name.Text;
PlayerChooser.player2Name = textBoxPlayer2Name.Text;
TwoPlayers501_new twoPlayers501_new = new TwoPlayers501_new();
twoPlayers501_new.Show();
...
}
Run Code Online (Sandbox Code Playgroud)
以及发生异常的类和构造函数
public partial class TwoPlayers501_new : Window
{
public TwoPlayers501_new()
{
InitializeComponent();
textBlockPlayer1Name.Text = PlayerChooser.player1Name;
textBlockPlayer2Name.Text = PlayerChooser.player2Name;
}
...
}
Run Code Online (Sandbox Code Playgroud)
预先感谢,这可能有点琐碎...
请考虑以下代码:
public class LimitedBuffer<T> {
/// <summary>
///
/// </summary>
public const int DefaultSize = 10;
/// <summary>
///
/// </summary>
private T[] buffer;
/// <summary>
///
/// </summary>
private int poe;
/// <summary>
///
/// </summary>
private int pow;
/// <summary>
///
/// </summary>
/// <param name="size"></param>
public LimitedBuffer(int size = 10) {
// Buffer instantiation
this.buffer = new T[10];
// Value initialization
for (int i = 0; i < this.buffer.Length; i++)
this.buffer[i] = default(T);
this.pow = 0;
this.poe …Run Code Online (Sandbox Code Playgroud) WHYYYYYYY?
以下是我的一半代码,因为我已经删除了大部分代码以及我在尝试解决此问题时丢失的每一根头发.无论我删除或更改什么,我都会收到StackOverflowException.
这是非常奇怪的,因为这个完全相同的代码更早.
请给我任何建议,因为我很无能为力......
我检查了这个,我不认为这是发生的事情:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace ConsoleApplication10
{
class Program
{
public int currentDialogueID = 0;
List<NPC> NPCList = new List<NPC>() {
new NPC(0, "Man", 1, true),
new NPC(1, "Woman", 1, true),
new NPC(2, "Troll", 3, true)
};
static void Main(string[] args)
{
Program d = new Program();
d.Init();
Console.ReadKey(false);
}
void Init()
{
getNPCByName("Man").NPCDialogue();
}
public NPC getNPCByName(string npcName)
{
IEnumerable<NPC> myNPCsName …Run Code Online (Sandbox Code Playgroud) 除了初始化的普通类之外,我看不到任何其他内容.
这是类,类Bet.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab_ADayAtTheRaces
{
public class Bet : Form1
{
public int Bets_Joe;
public int Bets_Bob;
public int Bets_Al;
public int Dog_Joe;
public int Dog_Bob;
public int Dog_Al;
public int Amount;
public int Dog;
public Guy Bettor;
public string GetDescription()
{
Amount = (int)numericUpDownBucksToBet.Value;
Dog = (int)numericUpDownDogToBetOn.Value;
//Bettor =
return Bettor + " placed a bet in the amount of " + Amount + " bucks on dog number …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,结果是堆栈溢出.尽管两个字符串都有空字符,但strcpy循环应该终止,因为源字符串具有空字符.为什么发生堆栈溢出?
#include <stdio.h>
#include<strings.h>
int main(void) {
char *str="Hello world";
char *str1="Good morning";
strcpy(str,str1);
printf("%s",str);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是我的战士班.我想在这堂课上有一系列的战士.我们的想法是能够调用这样的方法 - > warrior.select(1),它将获得在索引1处创建的战士.希望这是有意义的.请向我解释为什么会发生这种错误.
Exception in thread "main" java.lang.StackOverflowError
at pking.Warrior.<init>(Warrior.java:42)
Run Code Online (Sandbox Code Playgroud)
码
package pking;
public class Warrior {
private String name;
private int age;
private String call;
private int attackPower;
private String weapon;
public Warrior(String myName, int myAge, String myCall, int myAttackPower) {
name = myName;
age = myAge;
call = myCall;
attackPower = myAttackPower;
weapon = "";
Warrior[] warriors = new Warrior[4];
warriors[0] = new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000);
warriors[1] = new Warrior("Crixus", 35, "CHAMPION OF CAPUA", …Run Code Online (Sandbox Code Playgroud) 我构建这个函数来检查列表中的所有"var"是否都是数字.这是我试图做的
(defun check6 (list)
(if (null list) 'TRUE)
(if (not (numberp(first list))) nil)
(check6 (rest list)))
Run Code Online (Sandbox Code Playgroud)
但总是我得到堆栈溢出.
告诉我为什么?
我有一个Stack_overflow在我的OCaml程序错误最近.如果我打开回溯,我看到异常是由"原始操作""pervasives.ml"引发的,第270行.我进入了OCaml源代码,看到第270行定义了函数@(即列表追加).我没有从回溯中获得任何其他信息,即使在我的程序中抛出异常也没有.我切换到字节码并尝试ocamldebug,它没有帮助(没有产生回溯).
我认为这是一个非常奇怪的情况.在我的程序中我使用列表的唯一地方是(a)构建包含整数1到1000000的列表,(b)按顺序遍历RBT并将结果放入列表中,以及(c)打印整数列表包含表面上1000000的数字.我已经测试了所有函数,并且它们都没有包含无限循环,我认为1000000甚至不是一个巨大的数字.此外,我已经在Haskell(GHC),Scala和SML(MLton)中尝试了相当于我的程序,并且所有这些版本在相当短的时间内完美地工作.那么,问题是,可能会发生什么?我可以调试吗?
stack-overflow ×10
c# ×4
c ×2
class ×2
.net ×1
allocation ×1
arrays ×1
debugging ×1
exception ×1
instance ×1
java ×1
javascript ×1
linux ×1
lisp ×1
ocaml ×1
pointers ×1
recursion ×1
stackpanel ×1
strcpy ×1
wpf ×1