简体   繁体   中英

"pointer being freed was not allocated" How can I find my wrong access to memory?

Firstly, I have to print the value of Funcion "Ranking" which of parameter is Stack!
But my compiler says在此处输入图像描述 I think that I delete memory which is not allocated. But, I can't find when I delete that memory.... Please help me the point I wrongly access memory.

PS I want to make a program that receive ints or strings and, put ints or strings into stack. And use Ranking function as a Stack, the result would be,
For ints, how many numbers in stack which exceed first number of stack.
For strings, how many strings in stack which are later order than first string of stack.(The order of string objects is determined in the lexicographic order.)
For example, if inputs are 4 1 +2 5 3 -1 6 and the results is 2 (because 5 and 6 are bigger than first 4)
For example, if inputs are "to be or not to be" the results is 0 ( because there is no later order string than "to")

#include <iostream>
#include <sstream>
#include <string>
#include <cctype>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>

const size_t SIZE = 100;

using namespace std;

template <class T>
class Stack
{
public:
    Stack(size_t size = SIZE);
    Stack(const Stack<T>& s);
    const Stack<T>& operator = (const Stack<T>& s); ~Stack();
    bool isfull();
    bool isEmpty();
    void push(T d);
    T pop();
    T Elem(size_t i);
//  int Ranking(T obj);
private:
    T *_data;
    size_t _top, _size;
};

template <class T>
Stack<T>::Stack(size_t size) : _top(0), _size(size)
{
    _data = new T[size];
}

template <class T>
Stack<T>::Stack(const Stack<T>& s) : _top(s._top), _size(s._size)
{
    if (_data) delete[] _data;
    _size = s._size;
    _top = s._top;
    _data = new T[_size];
    for (int i = 0; i < _size; ++i)
        _data[i] = s._data[i];
}

template <class T>
Stack<T>::~Stack()
{
    delete [] _data;
}

template <class T>
bool Stack<T>::isfull()
{
    return _top == _size;
}

template <class T>
bool Stack<T>::isEmpty()
{
    return _top == 0;
}

template <class T>
void Stack<T>::push(T d)
{
    if (! isfull())
        _data[_top++] = d;
}

template <class T>
T Stack<T>::pop()
{
    if (! isEmpty())
        return _data[--_top];
    throw -1;
}


template <class T>
T Stack<T>::Elem(size_t i)
{
    return _data[i];
}

template <class T>
int Ranking(T obj)
{
    int k = 0;
    while(!obj.isEmpty())
    {
        if(obj.Elem(0).compare(obj.pop()) < 0 )
            k++;
    }
    return k;
}

int main(void)
{
    int k = 0;
    int value = 0;
    int num;
    string s;
    string line;
    Stack<string> strs;
    Stack<string> temps;
    vector<int> nums;

    while (getline(cin, line))
    {
        istringstream ins(line);
        while (ins >> s)
        {
            strs.push(s);
        }
        nums.push_back(Ranking(strs));
    }
}

In the body of your copy constructor, _data is initially uninitialised. if (_data) has undefined behaviour, which you have observed as delete[] ing an arbitrary pointer value.

There is never a previous allocation in a new object, so you don't need to delete[] anything. You declare (in a non-traditional way) but don't define a copy assignment operator.

I've also implemented a move constructor, which can reduce the number of copies you need.

Most of the bodies of your constructors can go, or be moved to the member initialiser. It is better to use a pre-defined algorithm rather than do it yourself.

#include <algorithm>
#include <utility>

template <class T>
Stack<T>::Stack(size_t size) : _data(new T[size]), _top(0), _size(size)
{}

template <class T>
Stack<T>::Stack(const Stack<T>& s) : _data(new T[s._size]), _top(s._top), _size(s._size)
{
    std::copy_n(s._data, s._size, _data);
}

template <class T>
Stack<T>::Stack(Stack<T>&& s) : _data(std::exchange(s._data, nullptr)), _top(s._top), _size(s._size)
{
}

template <class T>
Stack<T>& Stack<T>::operator=(Stack<T> s) // nb: by-value argument, copies or moves
{
    std::swap(_data, s._data);
    _size = s._size;
    _top = s._top;
    return *this;
}

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