整型在Visual Studio 2012 c++中改变自己的值

Integer changing its own value in Visual Studio 2012 c++

本文关键字:改变 改变自己 自己的 c++ Visual Studio 2012 整型      更新时间:2023-10-16

该类的size变量在构造函数中被设置为0。只有当您向数组中添加或删除一个项目时,它才会增加或减少,在将其分成两半的情况下,大小被削减为1/2容量。

然而,我的大小变量似乎跑了,随机变成了随机数,就像516846,这不在它设置的大小范围内。我检查并按照我的程序进行,没有发现任何改变尺寸的东西,我知道尺寸和容量是在施工时设置的。

#pragma once
#include <string>
#include <iostream>
using namespace std;
template <typename ItemType>
class Node 
    {
    private:
        ItemType* items;
        int size;
        int capacity;
    public:
        Node* nextNode;
        Node* prevNode;
        Node(Node* pNode, Node* nNode, int limit)
        {
            prevNode = pNode;
            nextNode = nNode;
            capacity = limit;
            size = 0;
            if(capacity != 0)
                items = new ItemType[capacity];
            cout << "Node() capcity = " << capacity << " size = " << size << endl;
        };
        ~Node(void){};
        int getSize()
        {
            return size;
        };
        void addItem(int index, ItemType item)
        {
            cout << "node->addItem" << endl;
            for(int i = (getSize() - 1); i >= index; i--)
            {
                items[i + 1] = items[i];
            }
            items[index] = item;
            size ++;
        };
        void addItem(ItemType item)
        {
            cout << "node->addItem" << endl;
            items[getSize()] = item;
            size ++;
        };
        void deleteItem(int index)
        {
            cout << "node->deleteItem index = " << index << endl;
            for(int i = index; i < (getSize() - 1); i++)
            {
                items[i] = items[i+1];
            }
            size --;
        };

        void cleaveInHalf()
        {
            cout << "node->cleaveInHalf" << endl;
            size = capacity/2;
        };
        bool isFull()
        {
            return ((getSize() >= capacity) ? true : false);
        };
    };

当调用isFull()函数时,我得到错误"读取位置0x00000004的访问冲突",大小是一些奇怪的数字,如51515615。

    void insert(int index, const ItemType& item) 
    {
        cout << "lal->insert" << endl;
        if (index > size)
            return;
        //if we have no nodes to hold data make a new one between head and tail
        if (head->nextNode == tail)
        {
            linkNewNode(head, tail);
        }
        // lets find the node to put it in and the spot in the array of the node
        int indexIntoArray = 0;
        Node<ItemType>* temp = getNodeContainingIndex(index, indexIntoArray);
        if(temp->isFull())
        {
            // if we are full then we ant to split, and then call this function again to find the new location to go in.
            splitNode(temp);
            insert(index, item);
            return;
        }
        // and now insert it in
        cout << "lal->----attempting to add item at " << indexIntoArray << endl;
        temp->addItem(indexIntoArray, item);
        size ++;
    };
    Node<ItemType>* getNodeContainingIndex(int index, int& indexIntoArray)
    {
        cout << "lal->getNodeContainingIndex" << endl;
        Node<ItemType>* temp;
        if (index == size)
        {
            temp = tail->prevNode;
            indexIntoArray = temp->getSize();
        }
        else if (index <= (size/2)) /* coming from 0*/
        {
            cout << "lal->----coming from 0" << endl;
            int position = 0;
            temp = head->nextNode;
            position = position + temp->getSize();
            while (position < index)
            {
                temp = temp->nextNode;
                position = position + temp->getSize();
            }
            indexIntoArray = index - (position - temp->getSize());
            return temp;
        } 
        else /*coming from size*/
        {
            cout << "lal->----coming from size = " << size << endl;
            int position = size;
            temp = tail;
            while (position > index)
            {
                temp = temp->prevNode;
                position = position - temp->getSize();
            }
            indexIntoArray = abs(position - index);
            return temp;
        }
    }
    Node<ItemType>* linkNewNode(Node<ItemType>* prev, Node<ItemType>* next)
    {
        cout << "lal->linkNewNode" << endl;
        Node<ItemType>* temp = new Node<ItemType>(prev, next, arrayCapacity);
        prev->nextNode = temp; next->prevNode = temp;
        numOfNodes ++;
        return temp;
    }

它是在getNodeContainingIndex函数中中断的,就在这里

int indexIntoArray = 0;
Node<ItemType>* temp = getNodeContainingIndex(index, indexIntoArray);
if(temp->isFull())
{

在temp->isFull()行

您正在获得的错误正在发生,因为指向您正在调用isFullLinkedArrayList的指针为空。我可以告诉你,因为你在0x00000004错误处遇到了访问冲突;这些数字的访问违规就是这样造成的。4是来自size从你的对象开始的偏移量;size的值没有意义,因为你的指针是垃圾。

看起来有一个路径通过getNodeContainingIndex不返回值(其中index == size);这可能会导致你的问题。但实际上,您最好在调试器中逐步执行,看看它在做什么。