std::vector 的最大大小 [英] maximum size of std::vector

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

问题描述

我在我的代码中使用了 vector

I am using vector in my code

    std::vector<CEventLogInfo >

    class CEventLogInfo 
    { 
      // date and time
  unsigned short    m_sMonth;
  unsigned short    m_sDay;
  unsigned int  m_nYear;
  unsigned short    m_sHour;
  unsigned short    m_sMin;
  unsigned short    m_sSec;

  unsigned long m_nGatewayMacID;
  unsigned char m_byCommandType;
  unsigned char m_byStatus;
  unsigned char m_byEventName;
  unsigned char m_byDirection;
  unsigned short    m_nPacketLen;
  char*         m_pPacket;
     // ..some method 
 }
 CEventLogInfo::CEventLogInfo(const CEventLogInfo& refMessage)
 {      
m_sMonth        = refMessage.m_sMonth;
m_sDay          = refMessage.m_sDay;
m_nYear         = refMessage.m_nYear;
m_sHour         = refMessage.m_sHour;
m_sMin          = refMessage.m_sMin;
m_sSec          = refMessage.m_sSec;

m_nGatewayMacID = refMessage.m_nGatewayMacID;
m_byCommandType = refMessage.m_byCommandType;
m_byStatus      = refMessage.m_byStatus;
m_byDirection   = refMessage.m_byDirection;
m_byEventName   = refMessage.m_byEventName;
m_nPacketLen    = refMessage.m_nPacketLen;  
if ( m_nPacketLen!=0)
{
    m_pPacket       = new char[m_nPacketLen];
    memcpy(m_pPacket,refMessage.m_pPacket,m_nPacketLen);

}
else
    m_pPacket = NULL;

  }    


 void CEventLoggerBody::SetEventInfoList(EventInfoList& ListEventLog)
 {
EventInfoList::iterator itrEventLogInfo;
for ( itrEventLogInfo = ListEventLog.begin(); itrEventLogInfo != ListEventLog.end();itrEventLogInfo++)
{
    CEventLogInfo* pEventLogInfo = new CEventLogInfo(*itrEventLogInfo);


    m_ListEventLog.push_back(*pEventLogInfo);
}
 }

  1. 此处 m_nPacketLen 是可变的,但不会超过 22 个字节.
  2. 这个向量对于 2000 条记录(44*2000)88000 字节工作正常,但是当超出此范围时,它会崩溃.我测试了 5000 条记录,当调用 SetEventInfoList 时,它会在复制构造函数中崩溃.

问题是

  1. 一个向量可以容纳信息的最大容量是多少.
  2. 如果 vector 不支持这么多字节,那么我应该为此使用什么 STL 容器.

注意:我的项目中不允许使用第三方库,请为此提出纯 C++ 解决方案.

推荐答案

您的问题不在于向量的大小(虽然存在实际限制,但您离它们还很远).这很可能是由于您的代码中的某些错误在您创建更多对象时会暴露出来.

Your problem isn't the size of the vector (while there are practical limits, you're nowhere near them). It is most likely due to some bug in your code that gets exposed when you create more objects.

我建议在崩溃时检查堆栈跟踪,并可能将其添加到您的问题中.另一个好的策略是将您的代码减少到重现问题所需的绝对最小值,如果这不能帮助您找出问题,请在此处发布结果代码.

I would recommend examining the stack trace at the point of the crash, and perhaps adding it to your question. Another good strategy is to reduce your code to the absolute minimum that's needed to reproduce the problem and -- if that doesn't help you figure out the problem -- post the resulting code here.

这篇关于std::vector 的最大大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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