C ++中的指针未正确初始化

时间:2013-11-25 23:27:21

标签: c++ pointers

我正在尝试执行以下代码,一切都很好,除了一件事,那就是tellerArray [2]从未正确初始化,它总是给我带来问题,我不知道为什么。它给我带来了问题:当我多次调试代码时,我开始知道这个事实。

#include <iostream>
#include <stddef.h>

using namespace std;

class Customer {
public:
    void setTime(int time) { this->_time = time; }
    int getTime() { return this->_time; }
    void setNextCustomer(Customer *next) { this->_next = next; }
    Customer* getNextCustomer() { return this->_next;}
private:
    int _time;
    Customer *_next;
};


class Teller {
public:
    Teller();
    ~Teller();
    void addCustomer(Customer *customer);
    int totalCustomers();
    int totalTime();

private:
    Customer *head;
    Customer *tail;
};

Teller::Teller() {
    this->head = NULL;
    this->tail = NULL;
}

Teller::~Teller() {
    delete head;
    delete tail;
    head = NULL;
    tail = NULL;
}

void Teller::addCustomer(Customer *customer) {
    customer->setNextCustomer(NULL);
    if(head == NULL) {
        head = customer;
    } else {
        tail->setNextCustomer(customer);
    }
    tail = customer;
}

int Teller::totalTime() {
    int totalTime = 0;
    Customer *tempCust = new Customer;
    for(tempCust = head; tempCust != NULL; tempCust = tempCust->getNextCustomer()) {
        totalTime += tempCust->getTime();
    }
    return totalTime;
}

int Teller::totalCustomers() {
    int totalCustomers = 0;
    Customer *tempCust = new Customer;
    for(tempCust = head; tempCust != NULL; tempCust = tempCust->getNextCustomer()) {
        totalCustomers += 1;
    }
    return totalCustomers;
}


int getLeast(int, int, int, int);
int getMax(int, int, int, int);


int main(int argc, const char*argv[]) {


    Teller *tellerArray[4];

    // creating four tellers ( counters )
    Teller *tellerOne   = new Teller();
    Teller *tellerTwo   = new Teller();
    Teller *tellerThree = new Teller();
    Teller *tellerFour  = new Teller();

    tellerArray[0] = tellerOne;
    tellerArray[1] = tellerTwo;
    tellerArray[2] = tellerThree;
    tellerArray[3] = tellerFour;

    char wannaBuyAnother = 'n';
    int  duration = 0, minTime = 0, maxTime = 0, index = 0;

    do {
        cout<<"Enter duration of your transaction: ";
        cin>>duration;
        Customer *customer = new Customer;
        customer->setTime(duration);
        minTime = getLeast(     tellerOne->totalTime(),
                                tellerTwo->totalTime(),
                                tellerThree->totalTime(),
                                tellerFour->totalTime()     );

        for(index = 0; index < 4; index++) {
            if( (tellerArray[index]->totalTime()) == minTime ) {
                break;
            }
        }



        tellerArray[index]->addCustomer(customer);

        cout<<"You can stand in Queue "<<index + 1<<"\n";

        cout<<"Do you want to buy another Ticket(Y/N)? ";
        cin>>wannaBuyAnother;

    } while ( wannaBuyAnother == 'y' || wannaBuyAnother == 'Y' );


    cout<<"Number of Customers Deal By Every Teller\n";

    for(index = 0; index < 4; index++) {
        cout<<"T"<<index<< "= \t"<<tellerArray[index]->totalCustomers()<<"\n";
    }

    maxTime = getMax( tellerOne->totalTime(),
                            tellerTwo->totalTime(),
                            tellerThree->totalTime(),
                            tellerFour->totalTime()  );
    for(index = 0; index < 4; index++) {
        if( (tellerArray[index]->totalTime()) == maxTime ) {
            cout<<"TELLER "<<index+1<<" Deal Maximum Customers of the Day\n";
            break;
        }
    }

    return 0;
}





int getLeast(int first, int second, int third, int fourth) {
    int min = first;
    if( second < min ) {
        min = second;
    } else if ( third < min ) {
        min = third;
    } else if ( fourth < min ) {
        min = fourth;
    }
    return min;
}

int getMax(int first, int second, int third, int fourth) {
    int max = first;
    if( second > max ) {
        max = second;
    } else if ( third > max ) {
        max = third;
    } else if ( fourth > max ) {
        max = fourth;
    }
    return max;
}

这是我调试代码时的输出。

tellerArray[0]  Teller *    0xbffff308  
tellerArray[1]  Teller *    0x8048c64   
tellerArray[2]  Teller *    0x1 
tellerArray[3]  Teller *    0xffff

我的代码实际上在做的是使用链表(客户类)来创建队列(柜员类),然后根据每个队列的时间,确定在哪个队列中放置下一个客户?

2 个答案:

答案 0 :(得分:3)

初始化看起来很好。这些值很奇怪,但除非您有特定的调试版本,否则您不能总是依赖于报告的指针值是正确的。但是,由于程序中存在以下未定义的行为,它们可能已被破坏:

我注意到你永远不会将_next上的Customer指针初始化为NULL,也不会在将它添加到列表时设置它。所以你的list tail总是有一个未定义的_next指针。这很可能会给你带来麻烦。

您应该在Customer上创建默认构造函数,并将_next初始化为NULL。

我要提到的一个不相关的事情是,您的getLeastgetMax功能不起作用。你为什么不试试这个:

cout << getLeast(4, 3, 2, 1) << endl;
cout << getMax(1, 2, 3, 4) << endl;

答案 1 :(得分:2)

代码有点奇怪,我没看到代码如何匹配它应该做的描述。

但是不容易找到错误,请看这段代码

int Teller::totalTime() {
    int totalTime = 0;
    Customer *tempCust = new Customer;
    for(tempCust = head; tempCust != NULL; tempCust = tempCust->getNextCustomer()) {
        totalTime += tempCust->getTime();
    }
    return totalTime;
}

您的代码在任何时候都没有设置tempCust->_next的值,因此tempCust->getNextCustomer()会返回一个垃圾值,所以从这一点开始,所有的投注都会关闭,而您的代码最终可能会做任何事情。

坦率地说,我看不到你的代码的任何逻辑,所以我不知道如何解决它。至少我建议在Customer构造函数中将_next设置为NULL。

class Customer {
public:
    Customer() { this->_next = NULL; }
    ...
private:
    ...
    Customer *_next;
};