Reputation: 7630
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);
}
}
So the question is
Note:third party library is not allowed in my project,please suggest on pure c++ solution for this.
Upvotes: 1
Views: 4488
Reputation: 500903
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.
Upvotes: 8
Reputation: 1363
Mayby try to use object pool patern http://sourcemaking.com/design_patterns/object_pool ?
Upvotes: 1
Reputation: 141988
Your std::vector
contains a small number (5000) of pointers
. This is very unlikely to be pushing any boundaries.
Sorry. Your bug is in another castle (I suspect in the one labelled m_pPacket
).
Upvotes: 1
Reputation: 131897
Your elements in the vector
will always be 4 / 8 bytes each, depending if you're on a 32bit or 64bit system because you're storing pointers. 2k records will lead to 8kb / 16kb memory used, so 5k will lead to 16kb / 32kb That shouldn't be a problem whatsoever (embedded systems not counted). There is most likely a bug somewhere in your code.
Upvotes: 7