1

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. here m_nPacketLen is variable but won't go beyond 22 bytes.
  2. this vector is working fine for 2000 record(44*2000)88000 bytes but when it goes beyond this it is crashing.I tested it for 5000 records it is crashing in the copy constructor when SetEventInfoList is called.

So the question is

  1. what is the maximum capacity of a vector that it can hold information in it.
  2. If vector doesn't support this much bytes then what STL container I should use for this.

Note:third party library is not allowed in my project,please suggest on pure c++ solution for this.

Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133
  • 2
    AFAIK, std::vector has no limit on it's size??? Are you sure there's no mistake in your code elsewhere? – Tony The Lion Jun 07 '11 at 09:03
  • How you insert records in it? Show us some code. – Kiril Kirov Jun 07 '11 at 09:06
  • 1
    @Tony: That's what `vector::max_size()` is for! – Oliver Charlesworth Jun 07 '11 at 09:10
  • 1
    What does `m_pPacket` point to? Who allocated that piece of memory, in which way, and who is responsible for deleting it? Do you have a copy ctor for `CEventLogInfo`? – sbi Jun 07 '11 at 09:12
  • When things like this start crashing, it's generally a good idea to avoid pointers and revert to value-semantic operations (even if they can be slower), until the problem disappears. Last change often reveals the problem. Here, the two candidates are that m_pPacket is a pointer (try replacing it with a vector or `std::string`), and that the vector stores pointers and not objects (try using a `vector` directly). – Tony Delroy Jun 07 '11 at 09:12
  • @Oli: Yeah, but that value is not very realistic. I think it was just `numeric_limits::max() / sizeof(T)` or sth along those lines. – Xeo Jun 07 '11 at 09:12
  • 1
    Does the vector contain pointers or objects? The first line fragment appears to declare it as a vector of pointers (although with the end of the line missing I can't be sure), but the `push_back` near the end is pushing an object. One of these lines must be wrong. – Mike Seymour Jun 07 '11 at 13:12
  • If the vector does contain objects, are they correctly copyable? I notice that you create an array with `new[]` in the constructor: does the destructor delete this with `delete[]`? And do you have a copy constructor and assignment operator to correctly (re)allocate memory in the new object? Since the array has a maximum size, it might be easier to replace the pointer (`m_pPacket`) with an array of the maximum size (and a check that `m_nPacketLen` is within the maximum), which will be correctly copyable with no further work. – Mike Seymour Jun 07 '11 at 13:17
  • I guess you are right.Let me check it.Instead of comment make your reply as answer so that I can make it as answer – Vikram Ranabhatt Jun 08 '11 at 05:01
  • Vector contain object it is typo Mike.I have provided the code for Copy constructor.I think it is correct.I dont know why it is crashing in constructor.I am running the application in debug mode still i am not able to see call stack.after crash call stack is empty all the time. – Vikram Ranabhatt Jun 08 '11 at 05:05
  • One point notice in the copy constructor m_pPacket has address 0xcdcdcdcd instead of 0000000.in the default constructor i am assigning m_pPacket = NULL.I dont know why this is coming – Vikram Ranabhatt Jun 08 '11 at 05:11

4 Answers4

8

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
7

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.

Xeo
  • 129,499
  • 52
  • 291
  • 397
1

Your std::vector contains a small number (5000) of pointers. This is very unlikely to be pushing any boundaries.

  1. There is no limit imposed by the container itself.
  2. n/a.

Sorry. Your bug is in another castle (I suspect in the one labelled m_pPacket).

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
1

Mayby try to use object pool patern http://sourcemaking.com/design_patterns/object_pool ?

piotrek
  • 1,333
  • 4
  • 17
  • 35