std :: sort内存不足

编程入门 行业动态 更新时间:2024-10-11 07:28:53
本文介绍了std :: sort内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 当我使用std :: sort排序超过1000万个值时,遇到内存不足错误. 除了最明显的原因之外,这可能是其他原因.我该如何解决这种行为. 谢谢, Vishnu

Hi all, When I use std::sort for sorting more than 10 million values, I encountered out of memory error. What could be the reason for this other than the most obvious reason. How can i get through this behavior. Thanks, Vishnu

推荐答案

我无法想象sort算法需要那么多额外的内存. 我唯一能想到的是由于某种原因,比较和复制值可能会导致内存泄漏.您的值实际上是类的实例吗?如果是这样,那么赋值或复制构造函数之一可能会泄漏内存吗? 附言:您使用哪个容器存储排序后的值?为避免重新分配,以所需的大小对其进行预分配可能会有所帮助.除非是课程列表. I can''t imagine the sort algorithm requires that much additional memory. The only other thing I can think of is that for some reason comparing and maybe copying the values causes memory leaks. Are your values actually instances of a class? And if so, is it possible that one of the assignment or copying constructors leaks memory? P.S.: what container are you using to store the sorted values? it might be beneficial to preallocate it in the needed size to avoid reallocation. Unless it''s a list of course.

我的测试程序" My ''test program'' #include <iostream> #include <vector> #include <algorithm> #include <ctime> using namespace std; int main() { const int SIZE = 16000000; time_t starttime, endtime; vector <int> huge; starttime = time(NULL); for (size_t i=0; i<SIZE; i++) { huge.push_back(rand()); } for (size_t i=0; i<100; i++) cout << huge[i] << endl; sort(huge.begin(), huge.end()); cout << "----------------------------" << endl; for (size_t i=0; i<100; i++) cout << huge[i] << endl; endtime = time(NULL); cout << "elapsed seconds: " << (endtime - starttime) << endl; }

在我的32位系统上以340秒的速度运行. 我想您应该发布相关代码以获得更好的帮助.

ran in 340 seconds on my 32-bit system. I suppose you should post the relevant code to get better help.

除了最明显的原因外,没有其他原因.如何通过?使用较少的值.或具有更多内存和64位应用程序的64位系统. :-)
—SA
No reasons other than the most obvious reason. How to get through? Use less values. Or 64-bit system with more memory and 64-bit application. :-)
—SA

更多推荐

std :: sort内存不足

本文发布于:2023-11-30 06:09:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存不足   std   sort

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!