指针传染媒介 [英] Vector of pointers

查看:89
本文介绍了指针传染媒介的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



作为一个c ++新手,我想弄清楚自己的东西,我有一个

的问题,有人可以提供帮助。


这里是代码:


#include< iostream>

#include< vector>


使用命名空间std;


class测试

{

public:

测试();

};


测试::测试()

{

}


无效测试()

{

vector< Test * v;


for(int i = 0; i< 5000000; i ++)

{

v.push_back(new Test());

}


for(vector< Test *> :: iterator i = v.begin(); i!= v.end(); i ++)

删除* i;

}


int main()

{

for(int i = 0; i< 10; i ++)

test();


string a;

cin> a;


返回0;

}


因为我删除了指针在我的向量中我认为这段代码没有内存

的问题。但是当我使用visual stuido

2005进行编译时,它最终会在该循环结束时使用~25mb内存。所以我在cygwin上用gcc尝试了它的b $ b它运行得很好,最终的内存使用量:900kb。

(双峰达到100mb)

>
我错过了什么吗?在清理代码之前没有

的任何异常运行时代码是否会泄漏内存?


谢谢。

解决方案

" Antimon" < a ***** @ gmail.comwrote in news:1164060480.203230.172480

@ j44g2000cwa.googlegroups.com:


Hi ,

作为一个c ++新手,我想弄清楚自己的东西,我有一个

的问题,有人可以提供帮助。

这里是代码:


#include< iostream>

#include< vector>


使用命名空间std;


class测试

{

public:

测试();

};


测试::测试()

{

}


无效测试()

{

vector< Test * v;


for(int i = 0; i< 5000000; i ++)

{

v.push_back(new Test());

}


for(vector< Test *> :: iterator i = v.begin(); i!= v.end(); i ++)

delete * i;

}


int main()

{

for(int i = 0;我< 10; i ++)

test();


string a;

cin> a;


返回0;

}


因为我删除了向量中的指针我认为没有内存
$ b这个代码的$ b问题。但是当我使用visual stuido

2005进行编译时,它最终会在该循环结束时使用~25mb内存。所以我在cygwin上用gcc尝试了它的b $ b它运行得很好,最终的内存使用量:900kb。

(双峰达到100mb)

>
我错过了什么吗?在清理代码之前没有

的任何异常运行时,该代码是否会泄漏内存?



是的。您(正确)删除的内存并不一定会回到操作系统。在你所使用的任何
平台上的C ++运行时可能正在进行某种内存缓存......而不是将内存返回给操作系统(通常是一个 ;缓慢的操作,YMMV但是

等待它在需要时将其分配回程序。




Antimon写道:




作为一个c ++新手,我想弄清楚自己的东西我有a / b
问题,有人可以提供帮助。


这里是代码:


#include< ; iostream>

#include< vector>


使用命名空间std;


class Test

{

public:

Test();

};


测试::测试()

{

}


无效测试()

{

vector< Test * v;


for(int i = 0; i< 5000000; i ++)

{

v.push_返回(新测试());

}


for(vector< Test *> :: iterator i = v.begin();我!= v.end(); i ++)

删除* i;

}


int main()

{

for(int i = 0; i< 10; i ++)

test();


string a;

cin> a;


返回0;

}


因为我删除了我的向量中的指针我认为这段代码没有内存

的问题。但是当我使用visual stuido

2005进行编译时,它最终会在该循环结束时使用~25mb内存。所以我在cygwin上用gcc尝试了它的b $ b它运行得很好,最终的内存使用量:900kb。

(双峰达到100mb)

>
我错过了什么吗?在清理代码之前没有

的任何异常运行时代码是否会泄漏内存?



不,你没有错过任何东西。当分配的内存通过平台返回到堆中时,该语言不会强制要求

。这是实现定义的。


我建议使用try-catch块以防std :: bad_alloc被抛出。


int main ()

{

试试

{

//做东西

}

catch(const std :: exception& e)

{

std :: cerr<< " error:";

std :: cerr<< e.what()<< std :: endl;

}

}


非常感谢帮助,只是检查了一下是的,当其他东西

需要内存时,它开始将该内存返回给操作系统。

11月21日凌晨2:02,Salt_Peter < pj_h ... @ yahoo.comwrote:


Antimon写道:




作为一个c ++新手,我想弄清楚自己的东西,我有一个

的问题,有人可以提供帮助。


这里是代码:


#include< ; iostream>

#include< vector>


using namespace std;


class Test

{

public:

Test ();

};


Test :: Test()

{

}


void test()

{

vector< Test * v;


for(int i = 0; i< 5000000; i ++)

{

v.push_back(new Test());

}


for(vector< Test *> :: iterator i = v.begin(); i!= v.end(); i ++)

delete * i;

}

< blockquote class =post_quotes>
int main()

{

for(int i = 0; i< 10; i ++)

test();


string a;

cin> a;


返回0;

}


因为我删除了向量中的指针,我认为这段代码没有内存

的问题。但是当我使用visual stuido

2005进行编译时,它最终会在该循环结束时使用~25mb内存。所以我在cygwin上用gcc尝试了它b / b
它运行得很好,最终内存使用量:900kb。

(峰值到100mb双向)


我错过了什么?在清理代码之前没有

的任何异常运行时代码是否会泄漏内存?不,你没有错过任何东西。当分配的内存通过平台返回到堆中时,该语言不会强加



的要求。这是实现定义的。


我建议使用try-catch块以防std :: bad_alloc被抛出。


int main ()

{

试试

{

//做东西

}

catch(const std :: exception& e)

{

std :: cerr<< " error:";

std :: cerr<< e.what()<< std :: endl;

}


} - 隐藏引用的文字 - 显示引用的文字 - 隐藏引用的文字 - 显示引用的文字 -


Hi,
As a c++ newbie, i''m trying to figure out stuff mysself and i have a
problem, thougt someone could help.

Here''s the code:

#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test();
};

Test::Test()
{
}

void test()
{
vector<Test*v;

for (int i = 0; i < 5000000; i++)
{
v.push_back(new Test());
}

for (vector<Test*>::iterator i = v.begin(); i != v.end(); i++)
delete *i;
}

int main()
{
for (int i = 0; i < 10; i++)
test();

string a;
cin >a;

return 0;
}

Since i delete the pointers in my vector i thought there''s no memory
problems with this code. But when i compile it using visual stuido
2005, it ends up using ~25mb memory at the end of that loop. So i tried
it with gcc on cygwin and it worked just fine, final mem usage: 900kb.
(peaks to 100mb both ways)

Am i missing something? Can that code leak memory when it runs without
any exceptions before cleanup code?

Thanks.

解决方案

"Antimon" <an*****@gmail.comwrote in news:1164060480.203230.172480
@j44g2000cwa.googlegroups.com:

Hi,
As a c++ newbie, i''m trying to figure out stuff mysself and i have a
problem, thougt someone could help.

Here''s the code:

#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test();
};

Test::Test()
{
}

void test()
{
vector<Test*v;

for (int i = 0; i < 5000000; i++)
{
v.push_back(new Test());
}

for (vector<Test*>::iterator i = v.begin(); i != v.end(); i++)
delete *i;
}

int main()
{
for (int i = 0; i < 10; i++)
test();

string a;
cin >a;

return 0;
}

Since i delete the pointers in my vector i thought there''s no memory
problems with this code. But when i compile it using visual stuido
2005, it ends up using ~25mb memory at the end of that loop. So i tried
it with gcc on cygwin and it worked just fine, final mem usage: 900kb.
(peaks to 100mb both ways)

Am i missing something? Can that code leak memory when it runs without
any exceptions before cleanup code?

Yep. The memory that you (correctly) delete doesn''t necessarily get
handed back to the operating system. The C++ runtime on whatever
platform you''re on may be doing some sort of memory caching... not giving
the memory back to the OS (typically a "slow" operation, YMMV) but
holding on to it to allocate it back into the program when requested.



Antimon wrote:

Hi,
As a c++ newbie, i''m trying to figure out stuff mysself and i have a
problem, thougt someone could help.

Here''s the code:

#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test();
};

Test::Test()
{
}

void test()
{
vector<Test*v;

for (int i = 0; i < 5000000; i++)
{
v.push_back(new Test());
}

for (vector<Test*>::iterator i = v.begin(); i != v.end(); i++)
delete *i;
}

int main()
{
for (int i = 0; i < 10; i++)
test();

string a;
cin >a;

return 0;
}

Since i delete the pointers in my vector i thought there''s no memory
problems with this code. But when i compile it using visual stuido
2005, it ends up using ~25mb memory at the end of that loop. So i tried
it with gcc on cygwin and it worked just fine, final mem usage: 900kb.
(peaks to 100mb both ways)

Am i missing something? Can that code leak memory when it runs without
any exceptions before cleanup code?

Nope, you haven''t missed anything. The language does not impose a
requirement over when the allocated memory is returned to the heap by
the platform. Thats implementation defined.

I''ld suggest a try-catch block in case a std::bad_alloc is thrown.

int main()
{
try
{
// do stuff
}
catch( const std::exception& e )
{
std::cerr << "error: ";
std::cerr << e.what() << std::endl;
}
}


Thanks alot for helping, just checked it and yes when something else
needs memory, it starts to give that memory back to os.
On Nov 21, 2:02 am, "Salt_Peter" <pj_h...@yahoo.comwrote:

Antimon wrote:

Hi,
As a c++ newbie, i''m trying to figure out stuff mysself and i have a
problem, thougt someone could help.

Here''s the code:

#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test();
};

Test::Test()
{
}

void test()
{
vector<Test*v;

for (int i = 0; i < 5000000; i++)
{
v.push_back(new Test());
}

for (vector<Test*>::iterator i = v.begin(); i != v.end(); i++)
delete *i;
}

int main()
{
for (int i = 0; i < 10; i++)
test();

string a;
cin >a;

return 0;
}

Since i delete the pointers in my vector i thought there''s no memory
problems with this code. But when i compile it using visual stuido
2005, it ends up using ~25mb memory at the end of that loop. So i tried
it with gcc on cygwin and it worked just fine, final mem usage: 900kb.
(peaks to 100mb both ways)

Am i missing something? Can that code leak memory when it runs without
any exceptions before cleanup code?Nope, you haven''t missed anything. The language does not impose a

requirement over when the allocated memory is returned to the heap by
the platform. Thats implementation defined.

I''ld suggest a try-catch block in case a std::bad_alloc is thrown.

int main()
{
try
{
// do stuff
}
catch( const std::exception& e )
{
std::cerr << "error: ";
std::cerr << e.what() << std::endl;
}

}- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -


这篇关于指针传染媒介的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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