堆栈溢出的大阵,但不与同样大的载体? [英] Stack overflow with large array but not with equally large vector?

查看:130
本文介绍了堆栈溢出的大阵,但不与同样大的载体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我遇到了一个有趣的问题,大数据结构的工作。我最初是用一个向量来存储向上1000000整数,但后来我决定(一旦它被宣布反正我是保留百万点)实际上并没有需要载体的动态功能,这将是有益的,相反,能够在数据结构中添加值的任何地方。所以我把它切换到一个数组和 BAM 的堆栈溢出。我猜这是因为在编译时声明数组的大小将其放在堆栈并利用动态矢量,而不是把它放在堆(我猜是大?)。

那么,什么是正确的答案吗?回迁动态内存系统,这样它就会提上堆?增加堆栈的大小?还是我放​​在这里整个事情...?

大错特错

谢谢!


解决方案

  

我最初是用向量来存储向上1000000整数


好主意。


  

但后来决定我其实没有(只要我预留百万点,因为它被宣布反正)所需要的载体的动态功能


不是一个好主意。你没有需要它。


  

和这将是有益的,相反,可以在数据结构中添加值的任何地方。


我不明白。


  

我猜这是因为在编译时声明数组的大小将其放在堆栈并利用动态矢量,而不是把它放在堆(我猜是大?)。


大部分。调用堆栈通常的1MB,2MB的大小默认的顺序。您的堆(无存储)是唯一真正受可用内存限制。


  

那么,什么是正确的答案吗?回迁动态内存系统,这样它被放在堆?



  

增加堆栈的大小?


您可以,但即使你可以舒展4MB出来吧,你离开自己没有任何其他本地数据变量回旋余地。最好使用动态内存—这就是做相应的事情。


  

还是我放在这里,整个事情大错特错...?


没有。

I ran into a funny issue today working with large data structures. I initially was using a vector to store upwards of 1000000 ints but later decided I didn't actually need the dynamic functionality of the vector (I was reserving 1000000 spots as soon as it was declared anyway) and it would be beneficial to, instead, be able to add values any place in the data structure. So I switched it to an array and BAM stack overflow. I'm guessing this is because declaring the size of the array at compile time puts it in the stack and making use of a dynamic vector instead placed it on the heap (which I'm guessing is larger?).

So what's the right answer here? Move back to a dynamic memory system just so it gets put on the heap? Increase the size of the stack? Or am I way off base on the whole thing here...?

Thanks!

解决方案

I initially was using a vector to store upwards of 1000000 ints

Good idea.

but later decided I didn't actually need the dynamic functionality of the vector (I was reserving 1000000 spots as soon as it was declared anyway)

Not such a good idea. You did need it.

and it would be beneficial to, instead, be able to add values any place in the data structure.

I don't follow.

I'm guessing this is because declaring the size of the array at compile time puts it in the stack and making use of a dynamic vector instead placed it on the heap (which I'm guessing is larger?).

Much. The call stack is typically of the order of 1MB-2MB in size by default. Your "heap" (free store) is only really bounded by your available RAM.

So what's the right answer here? Move back to a dynamic memory system just so it gets put on the heap?

Yes.

[edit: Joachim's right — static is another possible answer.]

Increase the size of the stack?

You could but even if you could stretch 4MB out of it, you've left yourself no wiggle room for other local data variables. Best use dynamic memory — that's the appropriate thing to do.

Or am I way off base on the whole thing here...?

No.

这篇关于堆栈溢出的大阵,但不与同样大的载体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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