直接剪切和粘贴以下算法:
def msort[T](less: (T, T) => Boolean)
(xs: List[T]): List[T] = {
def merge(xs: List[T], ys: List[T]): List[T] =
(xs, ys) match {
case (Nil, _) => ys
case (_, Nil) => xs
case (x :: xs1, y :: ys1) =>
if (less(x, y)) x :: merge(xs1, ys)
else y :: merge(xs, ys1)
}
val n = xs.length / 2
if (n == 0) xs
else {
val (ys, zs) = xs splitAt n
merge(msort(less)(ys), msort(less)(zs))
}
}
Run Code Online (Sandbox Code Playgroud)
导致5000个长列表上的StackOverflowError.
有没有办法优化这个,以便不会发生这种情况?
public class Category {
private Category parentCategory;
private Set<Category> childCategories;
private String name;
public Category() {
childCategories = new HashSet<Category>();
}
public Category getParentCategory() {
return parentCategory;
}
public void setParentCategory(Category parentCategory) {
this.parentCategory = parentCategory;
}
public Set<Category> getChildCategories() {
return childCategories;
}
public void setChildCategories(Set<Category> childCategories) {
this.childCategories = childCategories;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category [childCategories=" + childCategories + …Run Code Online (Sandbox Code Playgroud) interface Pong<T> {}
class Ping<T> implements Pong<Pong<? super Ping<Ping<T>>>> {
static void Ping() {
Pong<? super Ping<Long>> Ping = new Ping<Long>();
}
}
Run Code Online (Sandbox Code Playgroud)
尝试编译这会给出错误:
The system is out of resources.
Consult the following stack trace for details.
java.lang.StackOverflowError
at com.sun.tools.javac.code.Types$23.visitClassType(Types.java:2579)
at com.sun.tools.javac.code.Type$ClassType.accept(Type.java:554)
at com.sun.tools.javac.code.Types$UnaryVisitor.visit(Types.java:3260)
at com.sun.tools.javac.code.Types$23.visitClassType(Types.java:2592)
at com.sun.tools.javac.code.Types$23.visitClassType(Types.java:2579)
at com.sun.tools.javac.code.Type$ClassType.accept(Type.java:554)
...
Run Code Online (Sandbox Code Playgroud)
作为Haskell的新手,我试图多次迭代一个函数(例如,逻辑映射).在命令式语言中,这将是一个简单的循环,但在Haskell中,我最终会出现堆栈溢出.以此代码为例:
main = print $ iter 1000000
f x = 4.0*x*(1.0-x)
iter :: Int -> Double
iter 0 = 0.3
iter n = f $ iter (n-1)
Run Code Online (Sandbox Code Playgroud)
对于少量迭代,代码可以工作,但是对于一百万次迭代,我得到了堆栈空间溢出:
Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize -RTS' to increase it.
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会这样.这里的尾递归应该没问题.也许问题是懒惰的评价.我尝试了几种方法来强制进行严格的评估,通过插入$!或seq在不同的位置,但没有成功.
什么是Haskell迭代函数很多次的方法?
我已经尝试过相关帖子的建议:这里或这里,但我总是以堆栈流程结束大量的迭代,例如main = print $ iterate f 0.3 !! 1000000.
我在刷新SlidingTray中的视图时遇到了崩溃(自定义的SlidingDrawer来自顶部).
我不确定是什么导致它..但我的第一个猜测是,这是由于嵌套布局的数量......
嵌套布局的最大数量是多少?
它是特定于设备的,我怎么能确定这是否是原因?
如果不是,那么这些东西都会导致它......是什么?
堆栈跟踪:
E/AndroidRuntime( 2199): FATAL EXCEPTION: main
E/AndroidRuntime( 2199): java.lang.StackOverflowError
E/AndroidRuntime( 2199): at android.graphics.Paint.measureText(Paint.java:1057)
E/AndroidRuntime( 2199): at android.text.Styled.drawDirectionalRun(Styled.java:267)
E/AndroidRuntime( 2199): at android.text.Styled.measureText(Styled.java:430)
E/AndroidRuntime( 2199): at android.text.Layout.measureText(Layout.java:1655)
E/AndroidRuntime( 2199): at android.text.Layout.getLineMax(Layout.java:689)
E/AndroidRuntime( 2199): at android.text.Layout.draw(Layout.java:340)
E/AndroidRuntime( 2199): at android.text.BoringLayout.draw(BoringLayout.java:365)
E/AndroidRuntime( 2199): at android.widget.TextView.onDraw(TextView.java:4168)
E/AndroidRuntime( 2199): at android.view.View.draw(View.java:6880)
E/AndroidRuntime( 2199): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
E/AndroidRuntime( 2199): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
E/AndroidRuntime( 2199): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
E/AndroidRuntime( 2199): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
E/AndroidRuntime( 2199): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
E/AndroidRuntime( 2199): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
E/AndroidRuntime( 2199): at android.view.View.draw(View.java:6883)
E/AndroidRuntime( 2199): …Run Code Online (Sandbox Code Playgroud) 我正在教自己使用gdb并运行一些随机测试.值得一提的是,我在Windows 7 x64上使用MinGW的便携式安装.我已经创建了一个程序,我知道它会导致堆栈溢出,当我在gdb中运行它时,我首先得到两个SIGSEGV信号(毫不奇怪),然后它退出(再次不出意外)代码030000000375.
Program received signal SIGSEGV, Segmentation fault.
Program received signal SIGSEGV, Segmentation fault.
Program exited with code 030000000375.
Run Code Online (Sandbox Code Playgroud)
好奇心让我最好......那个代码到底是什么?我用Google搜索并发现很少.
谢谢!
更新:作为参考我在Ubuntu上尝试了相同的程序,结果略有不同:
Program received signal SIGSEGV, Segmentation fault.
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
Run Code Online (Sandbox Code Playgroud) struct MemBlock {
char mem[1024];
MemBlock operator*(const MemBlock &b) const {
return MemBlock();
}
} global;
void foo(int step = 0) {
if (step == 10000)
{
global = global * MemBlock();
}
else foo(step + 1);
}
int main() {
foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
程序接收信号SIGSEGV,分段故障.foo中的0x08048510(步骤= 4000)at t.cpp:12 12 void foo(int step = 0){
似乎MemBlock()实例虽然还没有被调用,但是花费了大量的堆栈内存(检查gdb信息).
而当我使用时global = global * global,程序正常退出.
任何人都可以解释内在的机制吗?
以下是发生分段错误的代码段(不会调用perror):
job = malloc(sizeof(task_t));
if(job == NULL)
perror("malloc");
Run Code Online (Sandbox Code Playgroud)
更确切地说,gdb说segfault在__int_malloc调用内部发生,这是一个子例程调用malloc.
由于malloc函数与其他线程并行调用,最初我认为它可能是问题所在.我使用的是glibc 2.19版.
数据结构:
typedef struct rv_thread thread_wrapper_t;
typedef struct future
{
pthread_cond_t wait;
pthread_mutex_t mutex;
long completed;
} future_t;
typedef struct task
{
future_t * f;
void * data;
void *
(*fun)(thread_wrapper_t *, void *);
} task_t;
typedef struct
{
queue_t * queue;
} pool_worker_t;
typedef struct
{
task_t * t;
} sfuture_t;
struct rv_thread
{
pool_worker_t * pool;
};
Run Code Online (Sandbox Code Playgroud)
现在未来的实施:
future_t *
create_future()
{ …Run Code Online (Sandbox Code Playgroud) 几个星期前,Dragisa Krsmanovic 在这里问了一个关于如何在Scalaz 7中使用免费monad来避免堆栈溢出的问题(我已经调整了他的代码):
import scalaz._, Scalaz._
def setS(i: Int): State[List[Int], Unit] = modify(i :: _)
val s = (1 to 100000).foldLeft(state[List[Int], Unit](())) {
case (st, i) => st.flatMap(_ => setS(i))
}
s(Nil)
Run Code Online (Sandbox Code Playgroud)
我认为只是举起一个蹦床StateT应该工作:
import Free.Trampoline
val s = (1 to 100000).foldLeft(state[List[Int], Unit](()).lift[Trampoline]) {
case (st, i) => st.flatMap(_ => setS(i).lift[Trampoline])
}
s(Nil).run
Run Code Online (Sandbox Code Playgroud)
但它仍然打击堆栈,所以我只是将其作为评论发布.
Dave Stevens刚刚指出用应用程序*>而不是monadic 进行排序flatMap实际上运行得很好:
val s = (1 to 100000).foldLeft(state[List[Int], Unit](()).lift[Trampoline]) {
case …Run Code Online (Sandbox Code Playgroud) 我正试图让一个开发人员的应用程序在我的机器上工作.解决方案是使用Web API在VS 2015中构建的,我使用64位IIS Express运行它.每个请求都返回500.0错误.请求跟踪日志说明了这一点:
1517. -MODULE_SET_RESPONSE_ERROR_STATUS
ModuleName ManagedPipelineHandler
Notification EXECUTE_REQUEST_HANDLER
HttpStatus 500
HttpReason Internal Server Error
HttpSubStatus 0
ErrorCode Recursion too deep; the stack overflowed. (0x800703e9)
ConfigExceptionInfo
Run Code Online (Sandbox Code Playgroud)
相关的配置部分如下所示:
<system.webServer>
<handlers>
<remove name="OPTIONS" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
其他可能相关的事实:
我怎么会开始调试这个?我在Google上获得了相关的点击率.
stack-overflow ×10
c ×2
java ×2
scala ×2
android ×1
c++ ×1
compilation ×1
free-monad ×1
gdb ×1
haskell ×1
iis-express ×1
layout ×1
malloc ×1
mingw ×1
recursion ×1
scalaz ×1
trampolines ×1
url-routing ×1