指针值更改而不显式分配

Pointer value changes without explicit assignment

本文关键字:分配 指针      更新时间:2023-10-16

我有一个链表的工作示例,他在 0 处插入一个节点,然后打印出其值。但是,我的 print() 函数不适用于较大的数据,因为 NULL 检查无法产生所需的结果。在调试期间,我的指针成员current指向一个名为 nodeT 的类型。在我打印nodeT项的info成员后std::cout << *(current->info) << " ";当前值不再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;
}

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

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

您必须使用new运算符来分配新对象,例如

current = new nodeT<elemType>(insertItem);