我在if (currNode->right)
支票上遇到分段错误。谁能发现我犯的那个明显的错误?
struct node
{
int data;
node* left;
node* right;
};
void Preorder(node *root) {
std::stack<node*> currStack;
currStack.push(root);
while(currStack.top()) {
node *currNode = currStack.top();
currStack.pop();
if (currNode->right) {
currStack.push(currNode->right);
}
if (currNode->left) {
currStack.push(currNode->left);
}
// Print to console
printf("%d ", currNode->data);
}
}
它可能与我的主要功能有关,因此我将在下面添加它以供参考:
int main() {
node myNode;
myNode.data = 3;
node myNode1;
myNode1.data = 4;
node myNode2;
myNode2.data = 5;
node myNode3;
myNode3.data = 6;
node myNode4;
myNode4.data = 7;
myNode.left = &myNode1;
myNode.right = &myNode2;
myNode1.left = &myNode3;
myNode1.right = &myNode4;
Preorder(&myNode);
return 0;
}
答案 0 :(得分:1)
这是由于left
,right
和myNode2
中myNode3
和myNode4
指针的未初始化值。
为node
定义一个构造函数,你很好:
struct node
{
int data;
node* left;
node* right;
node() : data(0), left(nullptr), right(nullptr) {}
};
答案 1 :(得分:0)
#!/bin/bash
# this is a file checkout – do nothing
if [ "$3" == "0" ]; then exit; fi
BRANCH_NAME=$(git symbolic-ref --short -q HEAD)
NUM_CHECKOUTS=`git reflog --date=local | grep -o ${BRANCH_NAME} | wc -l`
#if the refs of the previous and new heads are the same
#AND the number of checkouts equals one, a new branch has been created
if [ "$1" == "$2" ] && [ ${NUM_CHECKOUTS} -eq 1 ]; then
git push origin ${BRANCH_NAME}
fi
似乎无效,例如空值。在条件中使用指针之前,应检查该情况。
这可能发生在您的顶层堆栈节点上?
答案 2 :(得分:0)
发现问题:将循环条件更改为<form id="uploadForm" action=*myBucketUrl* method="post" enctype="multipart/form-data">
<input type="hidden" name="AWSAccessKeyId" >
<input type="hidden" name="key">
<input type="hidden" name="Content-Type" >
<input type="hidden" name="acl" value="private">
<input type="hidden" name="policy">
<input type="hidden" name="signature">
</form>
答案 3 :(得分:-1)
实际上,我发现while(currStack.top())
是一个非常奇怪的情况。 top()
返回一个const引用,而不是一个指针(如果你曾期望获得NULL
)。但是如果引用的值是指针,并且它是NULL
,或者如果我们得到对int = 0的引用,则循环结束。
你确定那是你想要的吗?如果堆栈中没有元素,则可能会导致未定义的行为,但如果您预期的话,它肯定无法返回NULL
。