首页 \ 问答 \ Python Stack Corruption?(Python Stack Corruption?)

Python Stack Corruption?(Python Stack Corruption?)

我是python的新手(但不是编程),我无法解释以下行为。 似乎一个对象(“child”)中的变量(我的示例中的列表“children”)被一个完全不同的对象(“node”)中的该变量的值覆盖。 为了给出一些上下文,我正在尝试创建一个在树结构中使用的简单Node类。 该节点具有子节点和父节点(所有其他节点)。

我无法弄清楚为什么child.children获得与node.children相同的值。 他们以某种方式引用相同的数据吗? 为什么? 代码和输出如下:

class Node:
    children = []
    parent = 0
    visited = 0
    cost = 0
    position = (0, 0)
    leaf = 0

    def __init__(self, parent, pos):
        self.parent = parent
        self.position = pos

    def addChild(self, node):
        self.children += [node]

node = Node(0, (0,0))
child = Node(node, (3,2))

node.addChild(child)

print "node: ",
print node

print "node.childen: ",
print node.children

print "child: ",
print child

print "child.children",
print child.children

输出:

node:  <__main__.Node instance at 0x414b20>
node.childen:  [<__main__.Node instance at 0x414b48>]
child:  <__main__.Node instance at 0x414b48>
child.children [<__main__.Node instance at 0x414b48>]

如您所见,node.children和child.children都具有相同的值(包含child的列表),即使我只更新了node.children。 谢谢你的帮助!


I'm relatively new to python (but not to programming), and I can't explain the following behavior. It appears that a variable (the list "children" in my example) from one object ("child") is getting overwritten by the value for that variable in a completely different object ("node"). To give some context, I'm trying to create a simple Node class to use in a tree structure. The node has children and a parent (all other nodes).

I can't figure out why child.children gets the same value as node.children. Are they somehow referencing the same data? Why? Code and output follows:

class Node:
    children = []
    parent = 0
    visited = 0
    cost = 0
    position = (0, 0)
    leaf = 0

    def __init__(self, parent, pos):
        self.parent = parent
        self.position = pos

    def addChild(self, node):
        self.children += [node]

node = Node(0, (0,0))
child = Node(node, (3,2))

node.addChild(child)

print "node: ",
print node

print "node.childen: ",
print node.children

print "child: ",
print child

print "child.children",
print child.children

Output:

node:  <__main__.Node instance at 0x414b20>
node.childen:  [<__main__.Node instance at 0x414b48>]
child:  <__main__.Node instance at 0x414b48>
child.children [<__main__.Node instance at 0x414b48>]

As you can see, both node.children and child.children have the same value (a list containing child) even though I only updated node.children. Thanks for any help!


原文:https://stackoverflow.com/questions/8627628
更新时间:2023-08-23 12:08

最满意答案

第7行(指您的链接代码)。 这是一个简单的错误,在基于0的数组的语言中很常见,通常你必须从大小中减去一个来确定上限。

您声明“Board_Size”为10,但减去1得到9x9矩阵:

#define Board_Size 10 char P1_board [Board_Size - 1] [Board_Size - 1];

这会打印一个10x10矩阵:

printf(“1 |%c |%c |%c |%c |%c |%c |%c |%c |%c |%c | \ n”,P1_board [0] [0],P1_board [0 ] [1],P1_board [0] [2],P1_board [0] [3],P1_board [0] [4],P1_board [0] [5],P1_board [0] [6],P1_board [0] [ 7],P1_board [0] [8],P1_board [0] [9]);

C以行优先顺序存储数组(并且对于9x9矩阵元素A(0,10))与元素A(1,0)位于相同的存储位置,这就解释了为什么您在板上看到多个船只。

另外,C将字符常量视为整数,因此您可以编写(字母“a”)将字母转换为索引。

如果您不熟悉该术语,WIKIpedia对行排序的含义有很好的描述: https//en.wikipedia.org/wiki/Row-_and_column-major_order


Line #7 (referring to your linked code). It's a simple mistake and a common one in languages with 0-based arrays where you often have to subtract one from the size to determine the upper bound.

You declare "Board_Size" as being 10 but subtract one to get a 9x9 matrix:

#define Board_Size 10 char P1_board[Board_Size - 1][Board_Size - 1];

This prints a 10x10 matrix:

printf(" 1 | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n", P1_board[0][0], P1_board[0][1], P1_board[0][2], P1_board[0][3], P1_board[0][4], P1_board[0][5], P1_board[0][6], P1_board[0][7], P1_board[0][8], P1_board[0][9]);

C stores arrays in row-major order (and for a 9x9 matrix element A(0,10) is in the same memory location as element A(1,0), and that explains why you see multiple ships on the board.

As an aside, C treats character constants as integers, so you can write (letter - 'a') to convert a letter to an index.

WIkipedia has a nice description of what row-major order means if you're not familiar with the term: https://en.wikipedia.org/wiki/Row-_and_column-major_order

相关问答

更多

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)