gpt4 book ai didi

c++ - 指针值在没有显式赋值的情况下发生变化

转载 作者:行者123 更新时间:2023-11-28 05:46:54 26 4
gpt4 key购买 nike

我有一个链表的工作示例,它在 0 处插入一个节点,然后打印出它的值。但是,我的 print() 函数不适用于较大的数据,因为 NULL 检查无法产生所需的结果。在调试我的指针成员期间 current指向一个名为 nodeT 的类型.在我打印 nodeT 之后项目的 info成员(member)身份std::cout << *(current->info) << " "; current 的值不再是 nodeT而是垃圾值 0xcccccccc。 current被初始化。为什么我的指针值在调试时被改变了?这是代码:

#include "nodeT.h"

using namespace std;

template <class elemType>
class linkedListSort
{
public:
void insertAt(int location, elemType& insertItem);
void print();

private:
//consider making const
nodeT<elemType> *beginningNode; // handle to the beginning of the list
nodeT<elemType> *current; // pointer to current node
int currentIndex; //int representing which node in the list current is pointing to
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list

};

插入一个项目。使用第一个实际项目插入代码(如果列表为空插入项目):

template <class elemType>
void linkedListSort<elemType>::insertAt
(int location, elemType& insertItem)
{
if (location < 0 || location >= maxSize)
cerr << "The position of the item to be inserted "
<< "is out of range" << endl;
else
if (length >= maxSize) //list is full
cerr << "Cannot insert in a full list" << endl;
else
{
if (currentIndex == -1 && current == NULL) { // if the list is empty
currentIndex++; //increment index to 0 (first item key)
current = &nodeT<elemType>(insertItem); //point current to new item (link defaulted to NULL--last item in list)
beginningNode = current;
length++;
}
else
if (currentIndex != -1 && current != NULL) { // if the list is non-empty
if (currentIndex == location - 1) { // if current already points to desired location
nodeT<elemType> rightNode = *(current->link);
current->link = &nodeT<elemType>(insertItem, &rightNode); // insert item after current
length++;
}
else { //traverse list until desired index
if (currentIndex < location - 1) { // if current points to item before desired location
for (int i = currentIndex; i < location; i++) { // increment until desired location
if (i == location - 1) {
nodeT<elemType> rightNode = *(current->link);
current->link = &nodeT<elemType>(insertItem, &rightNode); // insert item after current
length++;
break; //item inserted break out of traversal
}
current = current->link;
currentIndex++;
}
}
else { // start from the beginning
*(current) = *beginningNode;
currentIndex = 0;
for (int k = 0; k < location; k++) {
if (k == location - 1) {
nodeT<elemType> rightNode = *(current->link);
current->link = &nodeT<elemType>(insertItem, &rightNode); // insert item after current
length++;
break; //item inserted break out of traversal
}
current = current->link;
currentIndex++;
}
}
}
}
}
} //end insertAt

这是用于存储链表中项目的节点类:

template <class elemType>
class nodeT {
public:
nodeT(elemType& infoParam, nodeT<elemType> *linkParam); //standard
nodeT(elemType& infoParam); //if unlinked node (ex. last item)
nodeT();
elemType *info;
nodeT *link;
};

template<class elemType>
nodeT<elemType>::nodeT(elemType& infoParam, nodeT<elemType> *linkParam) {
info = &infoParam;
link = linkParam;
}

//when link is null (last item and uncircular)
template<class elemType>
nodeT<elemType>::nodeT(elemType& infoParam) {
info = &infoParam;
link = NULL;
}

//in case node is needed before info or link is known (default)
template<class elemType>
nodeT<elemType>::nodeT() {
info = NULL;
link = NULL;
}

这是指针似乎起作用的打印函数:

template <class elemType>
void linkedListSort<elemType>::print()
{
//start from the begginning of the list
*(current) = *beginningNode;
for (int i = length; i > 0; i--) {
std::cout << *(current->info) << " "; //current seems to change value after this line executes
if (current->link != NULL) //link is not null because pointer somehow changed from nodeT to 0xcccccccc
current = current->link;
else
break;
}
//reset index and current
*(current) = *beginningNode;
currentIndex = 0;

std::cout << endl;
}

这是主要的:

#include <iostream>
#include "linkedListSort.h"
int main() {

linkedListSort<int> list1 = linkedListSort<int>(100);
int num = 0;
list1.insertAt(0, num);
list1.print();

char stop;
std::cin >> stop;
return 0;
}

请注意,此代码不会产生错误或异常。但是,我正处于开发过程中,我担心无法正确查看我正在写入链表实现的数据。如果您可以在 Debug模式下运行它并重新创建这个垃圾值/失败的空检查,请告诉我。为什么会这样?

最佳答案

&nodeT<elemType>(insertItem)&nodeT<elemType>(insertItem, &rightNode)正在获取临时对象的地址,这些地址将在评估该方法后消失。

你必须使用 new分配新对象的运算符,如

current = new nodeT<elemType>(insertItem);

关于c++ - 指针值在没有显式赋值的情况下发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com