草庐IT

c++ - Visual Studio 2010 调试 "if (var == NULL)"未触发

coder 2024-02-03 原文

已解决 - 构造函数问题

Matthew FlaschenMichael Burr指出 Node(int) 的重载构造函数调用 Node() 不起作用的问题 because... 谢谢大家!


我已经构建了一个程序(我正在调试它)并且遇到了一个奇怪的问题......一个 `if` 语句没有在它应该被触发的时候被触发......这是一个学校项目,我们必须构建一个具有至少一个“优化”功能的 AVL 树。

我确信并测试过 `rdown` 和 `ldown` 起作用(作为平衡因素)——树不是完全平衡的。相反,它基于分支的高度(即 - `balance()` 应该只返回 (1,0,-1) 否则它是不平衡的。

我希望这些信息足以解决这个奇怪的问题......我以前从未在 Microsoft Visual Studio 2010 中遇到过这样的事情。

节点结构:

struct Node {
    int data;           // the data in the Node
    int rdown;          // the number of ellements below the node on the right side
    int ldown;          // the number of ellements below the node on the left side
    Node * parrent;     // the node's parrent
    Node * lchild;      // the nodes left child
    Node * rchild;      // the nodes right child

    Node () { rdown = 0, ldown = 0; data = 0; parrent = NULL; lchild = NULL; rchild = NULL; }
    Node (int dat) {rdown = 0, ldown = 0; parrent = NULL; lchild = NULL; rchild = NULL; data = dat; } 
    bool end() { if (lchild == NULL && rchild == NULL) return true;     // check if this node is the 'end of the line' - where it doesn't
                 return false; }                                        // have any children
    bool goodToAdd() { if (lchild == NULL || rchild == NULL) return true;   // make sture the current node has at least one spot to add
                       return false; }                                      // a new node to - either lchild or rchild must be NULL

    int balance() { return (ldown - rdown); }                           // get a balance number for the node
};

导致问题的搜索功能

Node * AVL_Tree::search(const Node * num) {
 Node * tmpNode = AVL_Tree::root;      // tmpNode is a place holder for the search
 for (int i = 1; true; i++) {     // increment int i to check for excess searching -> pervents endless loop
  if (tmpNode == NULL) //****** causing problems********  // the search has reached a dead end (the data is not contained) ==>  NULL
   return NULL;
  if (tmpNode->data == num->data)   // if the data of num is the same as tmpNode the data is contained ==>  Node *
   return tmpNode;
            // since the node has not been found yet move down the tree...
  if (tmpNode->data > num->data && tmpNode->lchild != NULL) // if the data is smaller than the tmpNode move to the lchild
   tmpNode = tmpNode->lchild;
  else if (tmpNode->rchild != NULL)    // since the node has been proven to not be = to the data to be searched for
   tmpNode = tmpNode->rchild;    // and it is not smaller... move to the right

  if (i > (root->ldown + 1) && i > (root->rdown + 1) ) {  // the while loop has searched suffecent time and has not ended
   string tmp = "the search incountered a critical error... aborting..."; // to prevent an endless loop the string error
   throw tmp;            // is thrown (should not happen) - indicates a broken tree
  }
 }
}

第一次遇到 for 循环

的屏幕截图

第二次遇到 for 循环

的屏幕截图

如果您在底部的“Autos”选项卡中注意到所有数据和节点本身的地址都是 NULL - 但在下一个屏幕截图中它会继续

节目继续!!!什么?>!

我按下 F-10(“转到下一个命令”按钮)……它直接跳过语句?为什么?

最佳答案

0xcdcdcdcd 不是 NULL 指针 - 该值在 MSVC 的调试版本中用于已分配但未初始化的内存。

参见 When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?了解更多详情。

问题的根源可能在于采用 int 参数的构造函数:

Node (int dat) { Node(); data = dat; } 

Node(); 语句最终什么也不做。此构造函数使结构的大部分成员未初始化。

关于c++ - Visual Studio 2010 调试 "if (var == NULL)"未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4200170/

有关c++ - Visual Studio 2010 调试 "if (var == NULL)"未触发的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  4. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  5. ruby - 触发器 ruby​​ 中 3 点范围运算符和 2 点范围运算符的区别 - 2

    请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby-on-rails - Rails - 乐观锁定总是触发 StaleObjectError 异常 - 2

    我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd

  8. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  9. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  10. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

随机推荐