【问题标题】:Integer changing its own value in Visual Studio 2012 c++ [closed]整数在 Visual Studio 2012 c++ 中改变自己的值 [关闭]
【发布时间】:2013-03-08 18:43:18
【问题描述】:

该类的大小变量在构造函数中设置为零。仅当您将项目添加到数组或删除一个项目时,它才会增加或减少,并且在这种情况下切割成两半,大小会减少到 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() 行。

【问题讨论】:

  • 拜托,拜托不要写return ((getSize() &gt;= capacity) ? true : false);之类的东西
  • 在构造函数中放置一个断点,当你在那里设置另一个数据断点时,值发生变化。
  • return (getSize() &gt;= capacity) 够了,我亲爱的朋友。该表达式已经返回一个布尔值。
  • @m0skit0: return getSize() &gt;= capacity; 也够了。
  • @KeithThompson 括号从来没有伤害过任何人。

标签: c++ random integer default-value


【解决方案1】:

您遇到的错误是因为指向您正在调用isFullLinkedArrayList 的指针为空。我可以这样说,因为您在 0x00000004 错误处遇到访问冲突;并且以这种方式导致具有看起来像这样的数字的访问冲突。 4 来自对象开头的 size 的偏移量; size 的值没有意义,因为你的指针是垃圾。

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

【讨论】:

  • 感谢您的帮助,罪魁祸首确实是缺少路径。
  • @WIllJBD 那不是罪魁祸首;这是一个症状。你需要问自己为什么,几次,才能找到真正的罪魁祸首。一方面,您试图调试太多代码。为什么你的代码中有空指针?为什么你的测试很少?等等
  • 另外,为什么你的编译器没有接受这个?你拒绝过警告吗?这么多你懒得看?您的目标应该是让您的代码编译器的错误为零。就个人而言,我通常会打开“将警告视为错误”,但我接受这并不总是可能的。
  • 如果你必须知道的话,我大部分时间都花在了java、python和objective c上。我非常习惯 eclipse 和 intelli-j 的方式。 Visual Studio 是我仍在学习的东西。问题是我从右到左搜索节点的方式,反之亦然。我需要删除返回空指针的路径,然后再拆分 2 条路径,一条从右,一条从左,最后在每条路径中设置一个递归算法,从末端遍历节点并找到正确的位置。所以我完全替换了一个函数,现在它看起来更好并且运行起来像一个魅力。
猜你喜欢
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多