std::vector 错误 DAMAGE: before Normal block (#51)

dmws1985 2008-11-26 11:42:17
#include <iostream>
#include <vector>
#include <windows.h>
#include <time.h>
#include <algorithm>
using namespace std;

bool change(int dd, int mm)
{
int value1 = rand();
int value2 = rand();

return value1 > value2;
}

void main()
{
vector<int> list;
time_t tt = GetTickCount();
tm *t = localtime(&tt);

for (int i = 0; i < 20; ++i)
{
list.push_back(i);
}

std::sort(list.begin(), list.end(), change);
for (i = 0; i < list.size(); ++i)
{
cout<<list[i]<<' ';
}
cout<<endl;
}

今天写代码的时候发现的一个问题,貌似是内存泄露,但实在找不到原因。。忘各位大虾指教一二。。
...全文
294 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
WingForce 2008-11-27
  • 打赏
  • 举报
回复
这个取决于sort的实现,因为stl的sort是用的是quick sort,考虑一下quick sort的实现就知道为什么了,而且是偶然情况下才会这样

有的sort在集合较小时会退化成insert sort,这种情况下,无论如何都不会出现前面的问题
dmws1985 2008-11-27
  • 打赏
  • 举报
回复
vector <int> list;
time_t tt = GetTickCount();
tm *t = localtime(&tt);

这几行先忽略。。
kingteng 2008-11-27
  • 打赏
  • 举报
回复
我觉得可以啊,谓词函数指定的比较为什么不可以呢?谁解释一下
dmws1985 2008-11-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hai040 的回复:]
这个>不固定
不能用来排序
在一次排序过程中,两个元素间的大小关系应该是确定的
[/Quote]

那是不是说sort时,vector的元素要有一定规律才行?
hai040 2008-11-27
  • 打赏
  • 举报
回复
这个>不固定
不能用来排序
在一次排序过程中,两个元素间的大小关系应该是确定的
kingteng 2008-11-27
  • 打赏
  • 举报
回复
难道是srand(time((unsigned)NULL));
dmws1985 2008-11-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kingteng 的回复:]
#include <iostream>
#include <vector>
#include <windows.h>
#include <time.h>
#include <algorithm>
using namespace std;

bool change(int dd, int mm)
{
srand(time(NULL));//这里
int value1 = rand();
int value2 = rand();

return value1 > value2;
}

void main()
{
vector <int> list;
time_t tt = GetTickCount();
tm *t = localtime(&tt);

for (int i = 0; i < 20; ++i…
[/Quote]

其实我一开始就是这样写的。。后来发现有时候就是会出错,所以我就尝试不设置种子,结果就能必现这个问题了。。
kingteng 2008-11-26
  • 打赏
  • 举报
回复
#include <iostream>
#include <vector>
#include <windows.h>
#include <time.h>
#include <algorithm>
using namespace std;

bool change(int dd, int mm)
{
srand(time(NULL));//这里
int value1 = rand();
int value2 = rand();

return value1 > value2;
}

void main()
{
vector <int> list;
time_t tt = GetTickCount();
tm *t = localtime(&tt);

for (int i = 0; i < 20; ++i)
{
list.push_back(i);
}

std::sort(list.begin(), list.end(),change);
for (i = 0; i < list.size(); ++i)
{
cout <<list[i] <<' ';
}
cout <<endl;
}
dmws1985 2008-11-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kingteng 的回复:]
#include <iostream>
#include <vector>
#include <windows.h>
#include <time.h>
#include <algorithm>
using namespace std;

bool change(int dd, int mm)
{
int value1 = rand();
int value2 = rand();

return value1 > value2;
}

void main()
{
vector <int> list;
time_t tt = GetTickCount();
tm *t = localtime(&tt);

for (int i = 0; i < 20; ++i)
{
list.push_back(i); …
[/Quote]

可是这样写不就失去了sort的意义了吗?
我这样写的目的就是让它随机排序的。。
kingteng 2008-11-26
  • 打赏
  • 举报
回复
#include <iostream>
#include <vector>
#include <windows.h>
#include <time.h>
#include <algorithm>
using namespace std;

bool change(int dd, int mm)
{
int value1 = rand();
int value2 = rand();

return value1 > value2;
}

void main()
{
vector <int> list;
time_t tt = GetTickCount();
tm *t = localtime(&tt);

for (int i = 0; i < 20; ++i)
{
list.push_back(i);
}

std::sort(list.begin(), list.end()/*, change*/); //这里
for (i = 0; i < list.size(); ++i)
{
cout <<list[i] <<' ';
}
cout <<endl;
}
dmws1985 2008-11-26
  • 打赏
  • 举报
回复
如果将change函数这么改一下又没有问题了
bool change(int dd, int mm)
{
int value1 = rand();
int value2 = rand();

return (value1 > value2) && (dd != mm);
}

好像是由于排序时两个指针指向了同一个内存而造成的,但为什么会出现指针指向同一个内存的情况呢?不明白。。
还有我用的是VC6环境。。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧