结构指针在“新”调用中给出已分配的地址?

时间:2018-04-05 00:43:18

标签: c++ templates struct

我在模板化的类Node中使用结构MyQueue

经过几个小时的调试后,我可以看到在stutter()成员函数中,我调用new并且返回的Node指针似乎已经在使用中,并且它会进入一个无限循环我试过typedef,并且使用结构也没有运气。

在调试过程中,很明显当尝试创建新节点以插入链表时,程序只是重用已经分配给其他节点的内存。

我的标题和实现如下,对stutter()的实现中注意到的行为有一些注释。

#pragma once

template <typename elemtype>
class MyQueue
{
public:
    MyQueue();
    ~MyQueue();
    void enqueue(elemtype item);
    elemtype dequeue();
    bool isEmpty();
    void stutter();
    void unStutter();

private:
    int size;
    struct Node{
        elemtype data;
        Node *next;
    };

    Node *head;
    Node *tail;
};

myqueue.cpp

#pragma once
#include "myqueue.h"

template <typename elemtype>
MyQueue<elemtype>::MyQueue()
{
    head = nullptr;
    tail = nullptr;
    size = 0;
}     

template <typename elemtype>
MyQueue<elemtype>::~MyQueue()
{
    Node *next;
    while (head != nullptr){
        next = head->next;
        delete head;
        head = next;
    }
}   

template <typename elemtype>
void MyQueue<elemtype>::enqueue(elemtype item){
    Node *tmp = new Node();
    tmp->data = item;
    tmp->next = nullptr;
    if (isEmpty()){
        head = tmp;
        tail = tmp;
    }else{
        tail->next = tmp;
        tail = tmp;
    }
    size++;
}

template <typename elemtype>
bool MyQueue<elemtype>::isEmpty(){
    return (size==0) ? true : false;
}

template <typename elemtype>
elemtype MyQueue<elemtype>::dequeue(){

     Node* old = head;
     elemtype item = head->data;
     head = head->next;
     size--;
     delete old;
     return item;
}

template <typename elemtype>
void MyQueue<elemtype>::stutter(){
    //function is trying to repeat every element in a linked list
    Node *insertedNode;  //if I do the full new declaration here, it gives me a new address, but I can't get another as I loop
    if (size ==0 ){
        return;
    }
    Node *current = head; //current is meant to be an iterator
    while(current != nullptr){
        insertedNode = new Node();  //this is setting the value of the ptr to = head --- no idea why.
      //  *insertedNode = *current; //Removed based on comments             
        insertedNode->data = current->data; //Updated to make proper assignment
        insertedNode->next = current->next; //Updated to make proper assignment
        current->next = insertedNode; //insert new node
        current = insertedNode->next; //move on skipping new node
        size++;
    }
}

template <typename elemtype>
void MyQueue<elemtype>::unStutter(){
    if (size ==0) {return;}
    Node *current = head;
    Node *trash = head;
    while(current->next != nullptr){
        if (current->data == current->next->data){
            trash = current->next;
            current->next = current->next->next;
            delete trash;
        }
        current = current->next;
    }
}

我完全被这一点困扰了。我以为我对指针有了很好的把握,但我只是不理解这种情况下的行为。

更新,包括main.cpp

#include <iostream>
#include "myqueue.h"

int main()
{
    std::cout << "Testing Queue implementation" << std::endl;
    MyQueue<int> q;
    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3);
    q.stutter();
    int tmpi;
    while (!q.isEmpty()){
        tmpi = q.dequeue();
        std::cout << tmpi << std::endl;
    }


    return 0;
}

1 个答案:

答案 0 :(得分:0)

我不确定为什么这是行为 - 我能够通过@neilbutterworth关于改变我在模板类实例化方面的评论来解决问题。我将我需要的类明确地放在实现的底部,它立即开始工作。