简体   繁体   中英

Dynamic memory array / program crashing

my program is using dynamic memory arrays and has functions enqueue (add at beginning and shift right) and dequeue (remove last) Show array etc. The code seems fine to me. But there is a problem when you run the program it crashes like 5 out of 10 times. I suppose it is because of wrong new / delete usage. Can someone please tell my why the program is crashing? And how to fix it?

#include <iostream>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

template <class T>
class Queue
{
public:

    Queue(int size);
    int add_value(T);
    void show_array();
    void enqueue(T);
    T dequeue();

private:

    T *temp;
    T *data;
    int size;
    int index;
};
template<class T>
Queue<T>::Queue(int s)
{
    data = new T[s];
    if (data == NULL)
    {
        cout << "Not enough memory, program ends." << endl;
        exit(1);
    }
    size = s;
    index = 0;
}
template<class T>
void Queue<T>::show_array()
{
    for (int i = 0; i < index; i++)
        cout << data[i] << ' ';

    cout << endl;
}
template<class T>
int Queue<T>::add_value(T value)
{
    if (index == size)
    return(1);
    else
    {
      data[index] = value;
      index++;
      return(0);
    }
}
template <class T>
void Queue<T>::enqueue(T X)
{
    for(int i = size; i > 0; i--)
    {
       data[i] = data[i-1];
    }
    data[0] = X;
}
template <class T>
T Queue<T>::dequeue()
{
    size -= 1;
    index -= 1;
    temp = new T[size];
    for (int i=0; i<size; i++)
        temp[i] = data[i];

    delete[] data;

    data = new T[size];
    data = temp;

    return * data;
}
int main()
{
    Queue<double>numbers(10);

    for (int i = 0; i < 10; i++)
      numbers.add_value(i);

    cout << "Array:" << endl;
    numbers.show_array();
    numbers.enqueue(9.2);
    numbers.enqueue(9.1);
    cout << endl;
    cout << "Array after enqueue:" << endl;
    numbers.show_array();
    cout << "Array after dequeue:" << endl;
    numbers.dequeue();
    numbers.dequeue();
    numbers.show_array();

    system("pause");
    return 0;
} 

You have several errors in your code.

First, this is a terribly inefficient method for implementing a queue since each insert will involve copying every element that is currently in the queue.

Second, you access elements out of bounds:

for(int i = size; i > 0; i--)
{
   data[i] = data[i-1];
}

Since your array is allocated as data = new T[size] , accessing data[size] is attempting to access 1 element passed the end of the array. That is UB.

Third, you have a memory leak:

T Queue<T>::dequeue()
{
    size -= 1;
    index -= 1;
    temp = new T[size];
    for (int i=0; i<size; i++)
        temp[i] = data[i];

    delete[] data;

    data = new T[size]; // leaked!
    data = temp;

    return * data;
}

It should just be data = temp .

You have a memory corruption in enqueue :

Here is how you should write the for loop:

for (int i = size - 1; i > 1; i--)

instead of

for (int i = size; i > 0; i--)

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