C ++无法迭代函数返回的对象数组,seg错误

尝试从动态区域进行迭代时出现段错误。我在阵列上使用IDE添加了“观察者”,这似乎超出了范围。

我正在尝试模拟一些数据,所以这是我的测试代码,运行良好(没有函数返回):

cout << "composite array test:" << endl; 
       int size = 27; 
       SpamValue** spamCompositesArray =  new SpamValue*[size];
        /*spamBool*/
        spamCompositesArray[0] = new SpamValue(spamBool);
        spamCompositesArray[0]->setValueBool(0);
        spamCompositesArray[1] = new SpamValue(spamBool);
        spamCompositesArray[1]->setValueBool(1);
        /*spamByte*/
        spamCompositesArray[2] = new SpamValue(spamByte);
         spamCompositesArray[2]->setValueByte(1);
        spamCompositesArray[3] = new SpamValue(spamByte);
         spamCompositesArray[3]->setValueByte(0);
         /*spamChar*/
        spamCompositesArray[4] = new SpamValue(spamChar);
        spamCompositesArray[4]->setValueChar('a');
        spamCompositesArray[5] = new SpamValue(spamChar);
        spamCompositesArray[5]->setValueChar('b');
        spamCompositesArray[6] = new SpamValue(spamChar);
        spamCompositesArray[6]->setValueChar('c');
        spamCompositesArray[7] = new SpamValue(spamChar);
        spamCompositesArray[7]->setValueChar('d');
        /*spamInt*/
        spamCompositesArray[8] = new SpamValue(spamInt);
        spamCompositesArray[8]->setValueInt(1);
        spamCompositesArray[9] = new SpamValue(spamInt);
        spamCompositesArray[9]->setValueInt(2);
        spamCompositesArray[10] = new SpamValue(spamInt);
        spamCompositesArray[10]->setValueInt(3);
        spamCompositesArray[11] = new SpamValue(spamInt);
        spamCompositesArray[11]->setValueInt(4);
        /*spamFloat*/
        spamCompositesArray[12] = new SpamValue(spamFloat);
        spamCompositesArray[12]->setValueFloat(1);
        spamCompositesArray[13] = new SpamValue(spamFloat);
        spamCompositesArray[13]->setValueFloat(2);
        spamCompositesArray[14] = new SpamValue(spamFloat);
         spamCompositesArray[14]->setValueFloat(3);
        spamCompositesArray[15] = new SpamValue(spamFloat);
         spamCompositesArray[15]->setValueFloat(4);
        /*spamDouble*/
        spamCompositesArray[16] = new SpamValue(spamDouble);
        spamCompositesArray[16]->setValueDouble(1);
        spamCompositesArray[17] = new SpamValue(spamDouble);
         spamCompositesArray[17]->setValueDouble(2);
        spamCompositesArray[18] = new SpamValue(spamDouble);
         spamCompositesArray[18]->setValueDouble(3);
        spamCompositesArray[19] = new SpamValue(spamDouble);
         spamCompositesArray[19]->setValueDouble(4);
        /*String*/
        spamCompositesArray[20] = new SpamValue(spamString); 
        const char* mercury = "mercury";
        spamCompositesArray[20]->setValueString(strdup(mercury));
        spamCompositesArray[21] = new SpamValue(spamString);
         const char* venus = "venus";
        spamCompositesArray[21]->setValueString(strdup(venus));
        spamCompositesArray[22] = new SpamValue(spamString);
         const char* earth = "earth";
        spamCompositesArray[22]->setValueString(strdup(earth));
        spamCompositesArray[23] = new SpamValue(spamString);
         const char* mars = "mars";
        spamCompositesArray[23]->setValueString(strdup(mars));
        /*spamShort*/
        spamCompositesArray[24] = new SpamValue(spamShort);
         spamCompositesArray[24]->setValueShort(0);
        spamCompositesArray[25] = new SpamValue(spamShort);
          spamCompositesArray[25]->setValueShort(1);
        spamCompositesArray[26] = new SpamValue(spamShort);
          spamCompositesArray[26]->setValueShort(2);


        for (int i=0; i<size; i++)
        {
            switch (spamCompositesArray[i]->getType())
           {
               case spamBool:
                   cout << "spamBool:" << endl; 
                   cout << spamCompositesArray[i]->getValueBool() << endl; 
                   break; 
               case spamChar:
                     cout << "spamChar:" << endl; 
                     cout << spamCompositesArray[i]->getValueChar() << endl; 
                   break; 
               case spamByte:
                     cout << "spamByte:" << endl; 
                     cout << spamCompositesArray[i]->getValueByte() << endl; 
                   break; 
               case spamInt:
                     cout << "spamInt" << endl; 
                     cout << spamCompositesArray[i]->getValueInt() << endl; 
                   break;
               case spamShort:
                       cout << "spamShort" << endl; 
                       cout << spamCompositesArray[i]->getValueShort() << endl; 
                   break; 
               case spamFloat:  
                   cout << "spamFloat" << endl; 
                   cout << spamCompositesArray[i]->getValueFloat() << endl;
                   break;
                case spamDouble:  
                     cout << "spamDouble" << endl; 
                     cout << spamCompositesArray[i]->getValueDouble() << endl;
                   break;
                case spamString:
                     cout << "spamString" << endl; 
                     cout << spamCompositesArray[i]->getValueString() << endl;
                break; 
            default:
                break; 
        }

然而,试图从一个函数中返回一个相似的对象数组,我发现段错误:

    SpamValue** spamQuery::spamQuery(int from_table, int to_table, int from_row, int to_row,
        int from_column, int to_column) {

    /*mock data */
    _comspositeArray = mockData();
    SpamValue** queryspamCompositesArray = NULL;
    int start_index = 0;
    int end_index = 0;
    /*within range?*/
    if (from_table >= 0 && from_column >= 0 && from_row >= 0 &&
            to_table<this->_n3 && to_column<this->_n2 && from_row<this->_n1) {
        /*get start index*/
        start_index = getIndex(from_table, from_row, from_column);
        /* get index*/
        end_index = getIndex(to_table, to_row, to_column);
        /*result size & return result array*/
        _query_result_size = end_index - start_index + 1;
        queryspamCompositesArray = new spamValue*[_query_result_size];
        //copy values
        int i = 0;
        for (i; i < _query_result_size; i++) {
            //****SEGFAULT HERE  queryspamCompositesArray[i] = _comspositeArray[i];
    //***it seems _comspositeArray[i] goes out of scope based on my IDEs variable watcher?


        }//end loop 

    }

    return queryspamCompositesArray;
}

这是mockData()函数:

spamValue** mockData()
{
      /*mock data start*/

    int mock_data_size = 32;

    SpamValue** spamCompositesArray = new SpamValue*[mock_data_size];
    /*spamBool*/
    spamCompositesArray[0] = new SpamValue(spamBool);
    spamCompositesArray[0]->setValueBool(0);
    spamCompositesArray[1] = new spamValue(spamBool);
    spamCompositesArray[1]->setValueBool(1);
    /*spamByte*/
    spamCompositesArray[2] = new SpamValue(spamByte);
    spamCompositesArray[2]->setValueByte(1);
    spamCompositesArray[3] = new spamValue(spamByte);
    spamCompositesArray[3]->setValueByte(0);
    /*spamChar*/
    spamCompositesArray[4] = new SpamValue(spamChar);
    spamCompositesArray[4]->setValueChar('a');
    spamCompositesArray[5] = new spamValue(spamChar);
    spamCompositesArray[5]->setValueChar('b');
    /*spamInt*/
    spamCompositesArray[6] = new SpamValue(spamInt);
    spamCompositesArray[6]->setValueInt(1);
    spamCompositesArray[7] = new spamValue(spamInt);
    spamCompositesArray[7]->setValueInt(2);
    /*spamFloat*/
    spamCompositesArray[8] = new SpamValue(spamFloat);
    spamCompositesArray[8]->setValueFloat(1);
    spamCompositesArray[9] = new spamValue(spamFloat);
    spamCompositesArray[9]->setValueFloat(2);
    /*spamDouble*/
    spamCompositesArray[10] = new SpamValue(spamDouble);
    spamCompositesArray[10]->setValueDouble(1);
    spamCompositesArray[11] = new spamValue(spamDouble);
    spamCompositesArray[11]->setValueDouble(2);
    /*String*/
       const char* mercury = "mercury";
    spamCompositesArray[12] = new SpamValue(spamString);
    spamCompositesArray[12]->setValueString(strdup(mercury));
    const char* venus = "venus";
    spamCompositesArray[13] = new SpamValue(spamString);
    spamCompositesArray[13]->setValueString(strdup(venus));
    /*spamShort*/
    spamCompositesArray[14] = new SpamValue(spamShort);
    spamCompositesArray[14]->setValueShort(0);
    spamCompositesArray[15] = new spamValue(spamShort);
    spamCompositesArray[15]->setValueShort(1);

    /*spamBool*/
    spamCompositesArray[16] = new SpamValue(spamBool);
    spamCompositesArray[16]->setValueBool(0);
    spamCompositesArray[17] = new spamValue(spamBool);
    spamCompositesArray[17]->setValueBool(1);
    /*spamByte*/
    spamCompositesArray[18] = new SpamValue(spamByte);
    spamCompositesArray[18]->setValueByte(1);
    spamCompositesArray[19] = new spamValue(spamByte);
    spamCompositesArray[19]->setValueByte(0);
    /*spamChar*/
    spamCompositesArray[20] = new SpamValue(spamChar);
    spamCompositesArray[20]->setValueChar('a');
    spamCompositesArray[21] = new spamValue(spamChar);
    spamCompositesArray[21]->setValueChar('b');
    /*spamInt*/
    spamCompositesArray[22] = new SpamValue(spamInt);
    spamCompositesArray[22]->setValueInt(1);
    spamCompositesArray[23] = new spamValue(spamInt);
    spamCompositesArray[23]->setValueInt(2);
    /*spamFloat*/
    spamCompositesArray[24] = new SpamValue(spamFloat);
    spamCompositesArray[24]->setValueFloat(1);
    spamCompositesArray[25] = new spamValue(spamFloat);
    spamCompositesArray[25]->setValueFloat(2);
    /*spamDouble*/
    spamCompositesArray[26] = new SpamValue(spamDouble);
    spamCompositesArray[26]->setValueDouble(1);
    spamCompositesArray[27] = new spamValue(spamDouble);
    spamCompositesArray[27]->setValueDouble(2);
    /*String*/
    spamCompositesArray[28] = new SpamValue(spamString);
    spamCompositesArray[28]->setValueString(strdup(mercury));
    spamCompositesArray[29] = new spamValue(spamString);
    spamCompositesArray[29]->setValueString(strdup(venus));
    /*spamShort*/
    spamCompositesArray[30] = new SpamValue(spamShort);
    spamCompositesArray[30]->setValueShort(0);
    spamCompositesArray[31] = new spamValue(spamShort);
    spamCompositesArray[31]->setValueShort(1);
    /*mock data end*/

}

我是C ++的新手,但我认为可以从C ++返回动态分配的数组(使用new)或静态数组。

另外,请不要建议使用矢量,因为我正试图对没有矢量的接口设计进行编程。

我只想知道如何将C ++对象数组作为“指向指针的指针”进行传递并对其进行迭代。那个

谢谢,非常感谢。

评论