0

我正在为多个队列进行模拟。我应该在开始时输入队列数,然后模拟所有这些。

每个“轮”的每个队列的输出是同时,服务的总数和每个队列的大小。

我的程序崩溃并且没有响应。

它写出第一个队列 och 然后崩溃......

帮助!

我认为我的计算是错误的,但我不知道,因为一切都崩溃了。

这里是:

#include <iostream>
#include <cstdlib>
#include <list>
#include <ctime>
#include<conio.h>
#include <time.h>
#include <stdlib.h>
#include<dos.h>
#include<windows.h>


using namespace std;

class Customer
{
public:

int servicet;
int served;

Customer()
{
    servicet= rand()%150+30;
}

int getServicetime()
{
    return servicet;
}

int getServed()
{
    return served;
}


int decreaseServeTime()
{
    servicet --;
}

};


int totServed=0;
int queues=0;
int inLine=0;
int totTime=0;
int smallestQueue=0;
int temp=0;
int ran=0;
double mean=0;
int served=0;
int serviceTime=0;
int help=0;
int sim=0;
int n=0;
using namespace std;
int main()
{
cout<<"Number of Cashiers?: "<<endl;
cin >> queues;
cout <<"How long simulation?: "<<endl;
cin >> sim;

list<Customer> *cashiers[queues];
list<Customer> *cust;


for(int i=0; i<=queues; i++)
{
    cust = new list<Customer>;
    cashiers[i] = cust;


}

srand(time(0));
while(n<sim)
{
    Sleep(2000);


    ran= rand()%4;
    smallestQueue = cashiers[0] ->size();

    for(int j=0; j<ran; j++)
    {
        for(int k=0; k<queues; k++)
        {
            temp = cashiers[k]->size();

            if(temp<=smallestQueue)
            {
                smallestQueue = temp;

                help=k;
            }
        }

        Customer C;

        cashiers[help]->push_back(C);
        inLine++;

    }

    for(int i=0; i<queues; i++)
    {
        if(serviceTime>0)
        {

            serviceTime = cashiers[i]->front().getServicetime();
            cashiers[i]->front().decreaseServeTime();

        }
        else if(serviceTime==0)
        {
            cashiers[i]->pop_front();
            served++;
        }
    }

    totTime++;
    int cash=1;
    for(int i=0; i<queues; i++)
    {
        if(inLine!=0)
        {
            cout <<"Kassa: "<<cash<<endl;
            inLine = cashiers[i]->size();
            mean = (totTime/inLine);
            totServed +=served;
            cash++;


        }
         cout <<inLine<<" "<<mean<<" "<<totServed<<endl;
    }


n++;
}

system("pause");




}
4

2 回答 2

2

我建议您使用诸如 Application Verifier 之类的程序来查找导致崩溃的问题:

http://www.microsoft.com/en-us/download/details.aspx?id=20028

学习如何调试软件并了解正在发生的事情非常重要。请在调试器(Visual Studio、Eclipse)中运行您的代码并查看它停止的位置。如果您使用了应用程序验证程序,那么它可能会在问题所在的地方停止。看看变量,看看它们是否有意义。看看你是否正在访问你不应该访问的内存位置。

要将 Application Verifier 与 Visual Studio 一起使用,请安装它,然后在 C:\Windows 的 System32 文件夹中找到 appVerifier.exe。然后打开文件并将其指向您的可执行文件。启用您认为正确的检查。然后在 Visual Studio 中运行它。

于 2012-12-10T17:53:02.597 回答
2

一个好的起点是调试器(例如gdb)。首先,我们在启用调试的情况下编译 ( g++ -ggdb) 并尝试在调试器中运行,

 $ g++ hi.cpp  -ggdb
 $ gdb ./a.out 
 GNU gdb (GDB) 7.5-ubuntu
 Copyright (C) 2012 Free Software Foundation, Inc.
 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
 This is free software: you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
 and "show warranty" for details.
 This GDB was configured as "x86_64-linux-gnu".
 For bug reporting instructions, please see:
 <http://www.gnu.org/software/gdb/bugs/>...
 Reading symbols from /home/ben/a.out...done.
 (gdb) run
 Starting program: /home/ben/a.out 
 Number of Cashiers?: 
 5
 How long simulation?: 
 5
 Kassa: 1

 Program received signal SIGSEGV, Segmentation fault.
 0x0000000000401827 in std::_List_const_iterator<Customer>::operator++ (
     this=0x7fffffffdd10) at /usr/include/c++/4.7/bits/stl_list.h:236
 236        _M_node = _M_node->_M_next;
 (gdb) backtrace
 #0  0x0000000000401827 in std::_List_const_iterator<Customer>::operator++ (
     this=0x7fffffffdd10) at /usr/include/c++/4.7/bits/stl_list.h:236
 #1  0x0000000000401665 in std::__distance<std::_List_const_iterator<Customer> >
     (__first=..., __last=...)
     at /usr/include/c++/4.7/bits/stl_iterator_base_funcs.h:82
 #2  0x0000000000401492 in std::distance<std::_List_const_iterator<Customer> > (
     __first=..., __last=...)
     at /usr/include/c++/4.7/bits/stl_iterator_base_funcs.h:118
 #3  0x000000000040135b in std::list<Customer, std::allocator<Customer> >::size
     (this=0x604010) at /usr/include/c++/4.7/bits/stl_list.h:855
 #4  0x0000000000401122 in main () at hi.cpp:125

在这里,我们看到程序因std::list. 编程一段时间后,您会直觉这可能是由于您的程序踩踏了一些不应该的内存。大致确定了问题的性质后,我们现在将切换到valgrind,这是一个专门跟踪此类问题的工具。

 $ valgrind ./a.out
 ==13751== Memcheck, a memory error detector
 ==13751== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
 ==13751== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
 ==13751== Command: ./a.out
 ==13751== 
 Number of Cashiers?: 
 5
 How long simulation?: 
 5
 Kassa: 1
 ==13751== Invalid read of size 8
 ==13751==    at 0x401422: std::list<Customer, std::allocator<Customer> >::begin() const (stl_list.h:749)
 ==13751==    by 0x40134F: std::list<Customer, std::allocator<Customer> >::size() const (stl_list.h:855)
 ==13751==    by 0x401121: main (hi.cpp:125)
 ==13751==  Address 0x5a06040 is 0 bytes inside a block of size 16 free'd
 ==13751==    at 0x4C2A44B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
 ==13751==    by 0x4018E7: __gnu_cxx::new_allocator<std::_List_node<Customer> >::deallocate(std::_List_node<Customer>*, unsigned long) (new_allocator.h:100)
 ==13751==    by 0x4017D9: std::_List_base<Customer, std::allocator<Customer> >::_M_put_node(std::_List_node<Customer>*) (stl_list.h:339)
 ==13751==    by 0x4015C0: std::list<Customer, std::allocator<Customer> >::_M_erase(std::_List_iterator<Customer>) (stl_list.h:1549)
 ==13751==    by 0x4013E9: std::list<Customer, std::allocator<Customer> >::pop_front() (stl_list.h:983)
 ==13751==    by 0x40108B: main (hi.cpp:113)
 ==13751== 
 ==13751== 
 ==13751== Process terminating with default action of signal 8 (SIGFPE)
 ==13751==  Integer divide by zero at address 0x402CCCE98
 ==13751==    at 0x40113C: main (hi.cpp:126)

在这里,我们看到valgrind告诉我们您的程序尝试对未分配的内存进行读取操作。特别是,这似乎是由于pop_front手术而发生的。查看源代码,您确实尝试在cashiers[i]没有首先检查它的大小的情况下弹出。

我们可以添加适当的检查,

 ...
 else if(serviceTime==0)
 {
     if (!cashiers[i]->empty()) {
         cashiers[i]->pop_front();
         served++;
     }
 }
 ...

然而,崩溃的实际原因是在计算 时除以零 ,如' 输出mean末尾所述。valgrind这是因为在计算时没有处理no Customersare的情况。inLinemean

于 2012-12-10T18:01:48.943 回答