简体   繁体   中英

Debug Assertion Failed, vector subscript out of range

my program should implement a threadsafe stack in c++ I get an error when I want to start the programm:

Debug assertion Failed! ...\\vector Line 1201

Expression: vector subscript out of range

I absolutely don't know what's wrong with the code

#include <condition_variable>
#include <iostream>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>

#define NUM_ELEMENTS 10

using namespace std;

class Stack
{
protected:
    vector<int> stack;
    int topOfStack;    
    int maxSize;
    mutable mutex _mutex;
    mutable condition_variable cv;

public:
    Stack(int size);
    int top() const; 
    int pop(); 
    void push(int element); 

    bool isFull() const;  
    bool isEmpty() const; 
};

Stack::Stack(int size)
{
    stack.reserve(size); 
    maxSize = size;      
    topOfStack = 0;
}

int Stack::top() const
{
    unique_lock<mutex> lck(_mutex);     
    
    // sperren, solange kein Wert da ist
    while (isEmpty())
        cv.wait(lck);

    int val = stack[topOfStack - 1];

    return val;
}

int Stack::pop()
{
    // Kritischer Bereich - sperren
    unique_lock<mutex> lck(_mutex);

    // sperren, solange kein Wert am Stack ist
    while (isEmpty())
        cv.wait(lck);

    // Wert zwischenspeichern und Stack-Pointer -1
    // danach werden wartende Threads benachrichtigt
    topOfStack--;
    int val = stack[topOfStack];
    cv.notify_all();

    return val;
}
void Stack::push(int element)
{
    // Kritischer Bereich - sperren
    unique_lock<mutex> lck(_mutex);

    // sperren, solange der Stack voll ist
    while (isFull())
        cv.wait(lck);

    // Wert speichern und Stack-Pointer +1
    // danach werden wartende Threads benachrichtigt
    stack[topOfStack] = element;
    topOfStack++;
    cv.notify_all();
}
bool Stack::isEmpty() const
{
    return topOfStack == 0;
}
bool Stack::isFull() const
{
    return topOfStack == maxSize;
}

// PRODUCER
class Producer
{
protected:
    shared_ptr<Stack> stack;

public:
    Producer(shared_ptr<Stack> stack);

    void produce(size_t amount);
};

//CONSUMER
class Consumer
{
protected:
    shared_ptr<Stack> stack;

public:
    Consumer(shared_ptr<Stack> stack);

    void consume(size_t amount);
};

Producer::Producer(shared_ptr<Stack> stack)
    : stack(stack)
{}
void Producer::produce(size_t amount)
{
    for (size_t i = 0; i < amount; i++)
    {
        cout << "Produce: " << i << endl;
        stack->push(i);
    }
}

Consumer::Consumer(shared_ptr<Stack> stack)
    : stack(stack)
{}
void Consumer::consume(size_t amount)
{
    for (size_t i = 0; i < amount; i++)
    {
        cout << "Consume: " << stack->pop() << endl;
    }
}

// ////////////////////////////////////////////////////////////////////////////

int main()
{
    shared_ptr<Stack> stack(new Stack(3));

    Producer producer(stack);
    Consumer consumer(stack);

    thread consumerThread(&Consumer::consume, &consumer, NUM_ELEMENTS);
    thread producerThread(&Producer::produce, &producer, NUM_ELEMENTS);

    consumerThread.join();
    producerThread.join();

    return 0;
}

Your vector<int> stack member is created with size 0 and forever maintains that size 0 . I don't see any attempts to change its size in your code. No wonder any index access to stack produces undefined behavior, including this "out of range" error.

Hint: if you want to change the size of an std::vector , the corresponding method is called resize , not reserve . The latter changes the capacity , not the size of a vector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM