that is not the same as the code when adding elements to a mass exceeding 5 values



  • There's a structure that stores std:vector

    struct TRemoteQuestData
    {
        bool bActivate;
        int nIndex;
        int nQuestID;
        bool bPreQuestCheckType;
        std::vector<int> nVecPreQuestCondition;
        TEventCondition GainCondition;
    };
    

    in this code, when more than five elements are reached in the mass, the sin occurs.

    TRemoteQuestData *pData = new TRemoteQuestData;
    memset( pData, 0, sizeof(TRemoteQuestData) );
    

    for( int j=0; j<10; j++ )
    {
    int nPreQuestID = pTable->GetFieldFromLablePtr( iIdx, sFieldNum._PreQuestCondition[j] )->GetInteger();
    if( 0 == nPreQuestID )
    continue;

    pData-&gt;nVecPreQuestCondition.push_back( nPreQuestID );
    

    }

    Crash in writing

    Вызвано исключение: нарушение доступа для чтения.
    _Pnext было 0x8.

    But if you write that, there's no mistake.

    TRemoteQuestData *pData = new TRemoteQuestData;
    memset( pData, 0, sizeof(TRemoteQuestData) );

    pData->nVecPreQuestCondition.resize(10);
    for( int j=0; j<10; j++ )
    {
    int nPreQuestID = pTable->GetFieldFromLablePtr( iIdx, sFieldNum._PreQuestCondition[j] )->GetInteger();
    if( 0 == nPreQuestID )
    continue;

    pData-&gt;nVecPreQuestCondition[j] = nPreQuestID;
    

    }

    Please explain why pushing_back is cracking and how to fix it, or is this how to use the 2nd option?



  • You've done it yourself. Better use the second option. It's better (in terms of code). Especially std:vector has an initial design that cannot use 0 with memset.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2