【问题标题】:Pointer seems to re-set for no apparent reason指针似乎无缘无故地重新设置
【发布时间】:2012-04-16 02:07:08
【问题描述】:

编辑:我也不介意知道这是否过于模棱两可,您无法理解或发现问题所在,我知道这是与代码没有直接关系,因为更改发生在我即将退出函数时......而不是在实际的“代码”期间。所以我希望这已经足够了,不管如果需要我会添加更多。提前谢谢!

我正在尝试制作一个简单的(单向)链表,我必须自己制作,而不是使用 STL 或任何其他已经制作的动态容器。我做了一个简单的,但我似乎有一个我无法解决的问题。

每当我在列表中创建一个新节点时(我总是返回列表的第一个节点以防它被更改,我不使用“哨兵”或“虚拟”节点)新节点是好的,直到当它自行重置到我介绍的第一个节点时,我到达了程序中的某个点。

为了尽量让我的帖子保持简洁,我将插入更少的代码和更多的“功能”或模式,但如果这还不够,我会添加更多。

这是它的工作原理:

我的节点项目

struct Node {    // simplified example
    string type;    //can be ignored
    int ap_nr;      // I sort by apartment number
    int sum;        // can also be ignored
    Node *next;
};

在 main 中,我将我的第一个节点初始化为“假”节点(ap_nr = -1,其他 ap_nr 不能为负数)。

int main ()
{
   int command = 1;   // used for loop
   Node *begin;    // head of my list
   begin = new Node;
   begin = create_node();   // initialize
   while (command != 0)    // This is for work with a "handler", accesses different mechanics of the program, I don't know exactly how to explain in english
   {
       input_command(command, begin);    // reads command, sends result to handler
   }
return 0;
}

这是我的输入命令:

void input_command (int &command,Node *begin)
{
    cout<<"Option #"; cin>>command;
    input_validation (command, begin);
}

我处理程序中唯一使用 atm 的部分:

void input_validation (int command, Node *begin)
{
     if (command == 1 trough 15)    // pseudo code 
         execute (command)    
}

在我将一个节点添加到列表后,我使用调试来跟踪它,第一个保持原样,但是第二个节点(我在第一个节点之前添加它并返回新的“开始”),在首先没关系(有一个新的内存位置和new_begin-&gt;next = old_begin,但在我退出ìnput_command 结构后它会重置)

*下面是input_command结构和程序图:*

主要: - 初始化“假”节点/进入菜单循环 -> 转到 input_command。

UI: - 输入命令 -> 读取命令 -> 验证 -> 发送到处理程序。

列表: - 将期望节点添加到“列表”(实际上没有列表本身,只是链接节点)-> 返回第一个节点。 第一个节点没问题(有新地址,指向旧节点)

返回UI:->输入命令(小括号)和resets(我的意思是无论我添加什么节点,它都会成为第一个我添加的节点。假设我介绍了公寓 1 到 10,它只记得“1”)。

我错过了什么吗?我的设计中有逻辑错误吗? 您需要更多代码来评估吗? 告诉我要添加什么,我意识到这有点模棱两可。

注意:现在我只能在我的主节点之前添加节点,我不会专注于任何新的东西,直到我让它正常工作,只要我的设计不是问题的一部分,我也不会改变它,但如果它不好,我会很感激注释和建议。

这是 add_node 函数唯一实现的部分:

Node *add_node (Node *begin,string type, int sum, int ap_nr) // Node begin e primul nod din lista, trebuie mereu plimbat cu toate functiile pe lista
{
// if first node is dummy node

if (begin->ap_nr == -1)
    {
        begin->type = type;
        begin->ap_nr = ap_nr;
        begin->sum = sum;
        begin->next = 0;
        return begin;
    }

 // else create new node and insert it in sorted position

 else
 {

     // if node should be inserted before first node (begin)

     if (ap_nr <begin->ap_nr)
     {
         Node *cursor;
         cursor = new Node;
         cursor->ap_nr = ap_nr;
         cursor->type = type;
         cursor->sum = sum;
         cursor->next = begin;
         return cursor;
     }
 }

【问题讨论】:

  • 请将源代码添加到 input_command 函数中。我怀疑你没有传递对它的引用,所以开始指针从未真正修改过。

标签: c++ singly-linked-list


【解决方案1】:

正如 RStrad 所说,您遇到的问题是您想要修改函数中的指针,并且将这些修改保留在 该函数之外

在 C/C++ 中,一切都是按值传递的。如果你想要一个函数来改变一些东西,你需要传递一个指向那个something的指针。因此,如果您想更改指针,则需要将指针传递给该指针,如下所示:

*Node begin;
input_command(command, &begin);
// the pointer itself has an address, which you are sending with &

input_command() 的定义将是:

void input_command (int &command, Node **begin)
// observe here the pointer to your pointer

input_command() 内部,您对input_validation 的呼叫必须是:

input_validation (command, &(*begin));
// the address of the pointer towards which **begin points

input_validation() 的定义将是:

void input_validation (int command, Node **begin)

...以及任何进一步的调用,例如对实际添加节点的函数的调用,将继续遵循这种格式。简而言之,如果你有一个指针*P,并且你想在一个函数中修改它,你需要发送它的地址&amp;P。该函数将接收指向指针**P 的指针,并且任何包含P 地址的进一步调用都必须使用&amp;(*P) 完成。

【讨论】:

    【解决方案2】:

    我真的需要看到将节点添加到列表代码中。根据您在当前头之前添加节点的描述,问题是 begin 仅在本地范围内被修改,并且没有从 input_command 传回(即按引用传递或按值传递)

    【讨论】:

    • 当我在拼贴画的时候,在我的国家它被认为是“非常好的”它实际上是垃圾,我对指针知之甚少,他们没有解释任何事情,也不会.我知道我应该自己学习这一点,但如果我确实学习了他们自己跳过的所有内容,那么来这里几乎没有意义。我的意思是我不知道如何正确传递指针,以便在内部和外部(在我当前的本地函数之外)对其进行修改
    • 它也重置为第一个分配的节点。如果问题是它保存在本地,为什么我的原始开始也不会在本地被记住,然后被销毁?但它不是......它持续存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多