本文介绍了push_back()做副本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我有一个套接字类CTestClientSocket,我用来模拟 负载测试。 我创建了多个客户端实例,如下所示: /> for(int i = 0; i< 5; i ++) { CTestClientSocket * pTemp = new CTestClientSocket(this, ip,port); pTemp-> Connect(); m_collClients.push_back(pTemp); //删除pTemp; } 注意我已经注释掉删除pTemp 然后我将一些数据发送到这样的服务器: //遍历客户集合 for(std :: vector< CTestClientSocket *> :: iterator it = m_collClients.begin(); it!= m_collClients.end(); it ++) { (* it) - > ;发送(byData); } 如果连接上的删除pTemp被注释掉,则此方法有效。但是我想b / b 认为push_back会复制pTemp。所以我可以 然后删除pTemp。但是,如果我删除pTemp,我会得到一个内存访问 问题(* it) - >发送(byData); 我必须清理在末尾。 push_back不会复制吗?我很困惑,有人可以解释一下。 还是有更好的办法来做这类事吗?I have a socket class CTestClientSocket which I am using to simulateload testing.I create multiple instances of the client like this:for (int i = 0; i < 5; i++){CTestClientSocket* pTemp = new CTestClientSocket(this, ip, port);pTemp->Connect();m_collClients.push_back(pTemp);//delete pTemp;}Note I have commented out the delete pTempThen I send some data to a server like this:// iterate through collection of clientsfor (std::vector<CTestClientSocket*>::iterator it =m_collClients.begin();it != m_collClients.end(); it++){(*it)->Send(byData);}This works if delete pTemp on the Connect is commented out. But Ithought that push_back would make a copy of pTemp. and so I couldthen delete pTemp. But if I delete pTemp I get an memory accessproblem in the (*it)->Send(byData);And I have to cleanup at the end. Does push_back NOT do a copy? I amconfused, can someone explain.Or is there a better way to do this sort of thing?推荐答案 2007年6月28日下午12:52,Angus写道:On 6/28/2007 12:52 PM, Angus wrote: 我有一个套接字我使用CTestClientSocket类来模拟 负载测试。 我创建了多个客户端实例: for(int i = 0; i< 5; i ++) { CTestClientSocket * pTemp = new CTestClientSocket(this,ip,port); pTemp-> Connect(); m_collClients.push_back(pTemp); //删除pTemp; } 注意我已经注释掉删除pTemp 然后我将一些数据发送到这样的服务器: //遍历客户集合 for(std :: vector< CTestClientSocket *> :: iterator it = m_collClients.begin() ; it!= m_collClients .end();它++) { (* it) - >发送(byData); } 如果Connect上的删除pTemp已被注释掉,则此方法有效。但是我想b / b 认为push_back会复制pTemp。I have a socket class CTestClientSocket which I am using to simulateload testing.I create multiple instances of the client like this:for (int i = 0; i < 5; i++){CTestClientSocket* pTemp = new CTestClientSocket(this, ip, port);pTemp->Connect();m_collClients.push_back(pTemp);//delete pTemp;}Note I have commented out the delete pTempThen I send some data to a server like this:// iterate through collection of clientsfor (std::vector<CTestClientSocket*>::iterator it =m_collClients.begin();it != m_collClients.end(); it++){(*it)->Send(byData);}This works if delete pTemp on the Connect is commented out. But Ithought that push_back would make a copy of pTemp. 如果复制指针会发生什么?And what happens if you copy a pointer ? 所以我可以 然后删除pTemp。但是,如果我删除pTemp,我会得到一个内存访问 问题(* it) - > Send(byData);and so I couldthen delete pTemp. But if I delete pTemp I get an memory accessproblem in the (*it)->Send(byData); 因为你有一个悬空指针。Because you have a dangling pointer. > And我最后要清理一下。 push_back不会复制吗?我很困惑,有人可以解释一下。 还是有更好的办法做这种事情吗?>And I have to cleanup at the end. Does push_back NOT do a copy? I amconfused, can someone explain.Or is there a better way to do this sort of thing? 使用boost :: shared_ptr<例如。 S. - Stefan Naewe stefan dot naewe at atlas-elektronik dot com 不要顶尖 http://www.catb.org/~esr/jargon/html/T/top-post.html 纯文本邮件,请 http:// www.expita.com/nomime.html Angus写道:Angus wrote: 我有一个socket类CTestClientSocket我用来模拟 负载测试。 我创建了多个客户端实例: for(int i = 0; i< 5; i ++) { CTestClientSocket * pTemp = new CTestClientSocket(this,ip,port); pTemp-> Connect(); m_collClients.push_back(pTemp) ; //删除pTemp; }I have a socket class CTestClientSocket which I am using to simulateload testing.I create multiple instances of the client like this:for (int i = 0; i < 5; i++){CTestClientSocket* pTemp = new CTestClientSocket(this, ip, port);pTemp->Connect();m_collClients.push_back(pTemp);//delete pTemp;} 当你的删除pTemp;时没有注释,你正在删除对象 你有新的东西。所以,你所拥有的是一个带有指针的向量,它指向不再存在的对象。When your "delete pTemp;" is uncommented, you''re deleting the objectsyou have new''d. So, what you have is a vector with pointers that dopoint to objects that no longer exist. 注意我已经注释掉了删除pTemp 然后我将一些数据发送到这样的服务器: //遍历客户集合 for(std :: vector< CTestClientSocket *> :: iterator it = m_collClients.begin(); it!= m_collClients.end(); it ++ ) { (* it) - >发送(byData); }Note I have commented out the delete pTempThen I send some data to a server like this:// iterate through collection of clientsfor (std::vector<CTestClientSocket*>::iterator it =m_collClients.begin();it != m_collClients.end(); it++){(*it)->Send(byData);} 当你尝试使用指向不存在的 对象的指针时,你会触发未定义的行为。And when you try to play around with pointers that point to non-existentobjects, you''re triggering undefined behaviour. > 如果Connect上的删除pTemp被注释掉,则此方法有效。但是我想b / b 认为push_back会复制pTemp。所以我可以 然后删除pTemp。但是,如果我删除pTemp,我会得到一个内存访问 问题(* it) - >发送(byData); 我必须清理在末尾。 push_back不会复制吗?我很困惑,有人可以解释一下。>This works if delete pTemp on the Connect is commented out. But Ithought that push_back would make a copy of pTemp. and so I couldthen delete pTemp. But if I delete pTemp I get an memory accessproblem in the (*it)->Send(byData);And I have to cleanup at the end. Does push_back NOT do a copy? I amconfused, can someone explain. 是的,在你的情况下,push_back会推送*指针*的副本。然后 你摧毁它指向的对象。 问候, Sumit。 - Sumit Rajan< su ********* @ gmail.com>Yes, push_back, in your case, pushes a copy of the *pointer*. And thenyou destroy the object it points to.Regards,Sumit.--Sumit Rajan <su*********@gmail.com> > 是的,在你的情况下,push_back会推送*指针*的副本。然后 你销毁它所指向的对象。>Yes, push_back, in your case, pushes a copy of the *pointer*. And thenyou destroy the object it points to. 正如Sumit Rajan所说,你只需复制指针,当你 写 push_back(* pTemp),它会按你期望的方式工作。As Sumit Rajan said, you just make a copy of the pointer, when youwritepush_back(*pTemp), it will works the way you expect. 这篇关于push_back()做副本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 10:56