整型在Visual Studio 2012 c++中改变自己的值
Integer changing its own value in Visual Studio 2012 c++
该类的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()行
您正在获得的错误正在发生,因为指向您正在调用isFull
的LinkedArrayList
的指针为空。我可以告诉你,因为你在0x00000004错误处遇到了访问冲突;这些数字的访问违规就是这样造成的。4是来自size
从你的对象开始的偏移量;size
的值没有意义,因为你的指针是垃圾。
看起来有一个路径通过getNodeContainingIndex
不返回值(其中index == size
);这可能会导致你的问题。但实际上,您最好在调试器中逐步执行,看看它在做什么。
相关文章:
- 变量没有改变?通过向量的函数调用
- 如何改变c++应用程序的视觉效果
- C++在不同线程中改变向量
- 如何访问和改变存储在矢量C++中的对象
- 在C++行尾写一个分号或多个分号是否会改变任何内容
- 为什么字符串的 move() 会改变内存中底层数据的位置?
- 为什么 c++ 动态数组的大小没有改变?
- 为什么提升图库的 read_graphviz() 函数会改变节点的索引
- 在向量内更改变量的值不会改变其在向量外的值
- 为什么 GCC 不能假设 std::vector::size 在这个循环中不会改变?
- 改变或缩放两个正态分布以具有特定的相关系数
- 当我使用CHAR_INFO结构时,控制台会无缘无故地改变颜色
- 为什么我的函数接受"std::string"进行排序不会改变它?
- MFC:你能在CDateTimeCtrl中改变自旋的加速度吗?
- 为什么切换 for 循环的顺序会显著改变执行时间?
- 使用可变参数模板改变模板参数
- 为什么左值不能改变自己。左值用户是什么意思?
- 类中的枚举在调用自己的函数时不会改变
- 当我读取文件时,由于某种原因,它自己改变了文件中的位置
- 整型在Visual Studio 2012 c++中改变自己的值