<badptr>循环错误</badptr>

时间:2014-01-29 21:49:16

标签: c++ visual-c++

每当我到达代码中时,我都会收到BadPtr错误:为什么?

for(int i = 0; i < processTable.size(); i++)
        {
            data_entry test = *processTable.at(i).dataCurrent; //ERROR
            cout << test.text << "\n";
        }

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

enum process_state { READY, RUNNING, WAITING };

struct data_entry {
    const char * text;
    int time;
};

struct process_entry
{
    int process;
    const data_entry * dataStart;
    const data_entry * dataEnd;
    const data_entry * dataCurrent;
    process_state state;
};

int _tmain(int argc, _TCHAR* argv[])
{
    vector<data_entry> dataTable;
    vector<process_entry> processTable;

    string line;
    ifstream myfile ("C:\\Users\\1162852\\Documents\\visual studio 2010\\Projects\\Project 1 (OS)\\Debug\\input.txt");
    if (myfile.is_open())
    {
        int processAmount = 0;

        while ( getline (myfile,line) )
        {
            if(line.find("START") != string::npos)
            {
                data_entry newEntry;
                newEntry.text = "START";
                newEntry.time = atoi(line.substr(6, 6).c_str());
                dataTable.push_back(newEntry);

                process_entry newProcessEntry;
                newProcessEntry.dataStart = &dataTable.at(dataTable.size() - 1);
                newProcessEntry.dataCurrent = &dataTable.at(dataTable.size() - 1);
                newProcessEntry.process = processAmount;
                processTable.push_back(newProcessEntry);

                processAmount += 1;
            }
            else if(line.find("END") != string::npos)
            {
                data_entry newEntry;
                newEntry.text = "END";
                newEntry.time = NULL;
                dataTable.push_back(newEntry);

                processTable.at(processTable.size() - 1).dataEnd = &dataTable.at(dataTable.size() - 1);
            }
            else if(line.find("RUN") != string::npos)
            {
                data_entry newEntry;
                newEntry.text = "RUN";
                newEntry.time = atoi(line.substr(4, 4).c_str());
                dataTable.push_back(newEntry);
            }
            else if(line.find("INPUT") != string::npos)
            {
                data_entry newEntry;
                newEntry.text = "INPUT";
                newEntry.time = atoi(line.substr(6, 6).c_str());
                dataTable.push_back(newEntry);
            }
            else if(line.find("SSD") != string::npos)
            {
                data_entry newEntry;
                newEntry.text = "SSD";
                newEntry.time = atoi(line.substr(4, 4).c_str());
                dataTable.push_back(newEntry);
            }
        }

        //reverse(dataTable.begin(),dataTable.end());
        /*for(int i = 0; i < dataTable.size(); i++)
        {
            cout << dataTable.at(i).text << "\n";
        }*/

        for(int i = 0; i < processTable.size(); i++)
        {
            data_entry test = *processTable.at(i).dataCurrent;
            cout << test.text << "\n";
        }
        myfile.close();
    }

    int test;
    cin >> test;
    return 0;
}

4 个答案:

答案 0 :(得分:2)

此赋值应返回数据条目类型。 使用->dataCurrent获取指向data_entry的指针,然后取消引用。 您可能还希望将data_entry指定为const,具体取决于它的用途。

const data_entry test = *(processTable.at(i)->dataCurrent);

答案 1 :(得分:1)

问题是您正在保存指向dataTable元素的指针,但如果向量调整大小,push_back将使这些指针无效。考虑将dataTableprocessTable作为共享指针的向量,这样它们就不会被移动。

vector<shared_ptr<data_entry>> dataTable;
vector<shared_ptr<process_entry>> processTable;

我还会将process_entry的成员更改为共享指针。通过这些更改,您的START部分现在看起来像这样:

            auto newEntry = make_shared<data_entry>();
            newEntry->text = "START";
            newEntry->time = atoi(line.substr(6, 6).c_str());
            dataTable.push_back(newEntry);

            auto newProcessEntry = make_shared<process_entry>();
            newProcessEntry->dataStart = dataTable.back();
            newProcessEntry->dataCurrent = dataTable.back();
            newProcessEntry->process = processAmount;
            processTable.push_back(newProcessEntry);

另一个选择是使用std::list,它不会在元素增长时复制元素。

答案 2 :(得分:0)

替换

*processTable.at(i).dataCurrent;

通过

*(processTable.at(i).dataCurrent);

此外,您没有复制ctor,因此制作了浅拷贝。 确保这是你想要的。

答案 3 :(得分:0)

您使用vectors循环内创建的局部变量填充了while,并且无法在封闭的if块中访问它们。

因此,一旦我们退出while块,所有局部变量都将被销毁,vector指向一堆bad_ptr s。