没有显式赋值的指针值更改

时间:2016-03-17 03:05:06

标签: c++ pointers

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

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

1 个答案:

答案 0 :(得分:1)

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

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

current = new nodeT<elemType>(insertItem);