[英]Issue with CDT in eclipse
我正在使用eclipse進行C ++程序。
在我運行代碼時構建我的代碼后,我得到name.exe已停止工作錯誤。 相同的代碼在http://codepad.org/2c5xFbLM正常工作。
請幫我找到這個問題。
提前致謝。
我的代碼:
#include<iostream>
#include<math.h>
#include <cstdlib>
using namespace std;
struct node{
struct node * lc;
struct node * rc;
int data;
};
typedef struct node Node;
Node * getNewNode(int data){
Node * node = NULL;
node = (Node*)malloc(sizeof(node));
node -> data = data;
node -> lc = NULL;
node -> rc = NULL;
return node;
}
Node * buildBst(Node * root,int data){
if(NULL == root){
return getNewNode(data);
}
if(data > root -> data){
root -> rc = buildBst(root->rc,data);
}else{
root -> lc = buildBst(root->lc,data);
}
return root;
}
void printInorder(Node * root){
if(root != NULL){
printInorder(root -> lc);
cout << root -> data << " ";
printInorder(root -> rc);
}
}
int main(int argc, char* argv[]) {
int arr [] = {2,3,4,1,5,9,0,3};
Node * root = NULL;
for(int i = 0;i < 6; ++i){
root = buildBst(root,arr[i]);
}
printInorder(root);
cout << endl;
}
由於您使用的是C ++,因此使用new
運算符而不是malloc
,崩潰發生在您調用malloc或第4個節點創建的第14次迭代時,因為通過調試進行額外的填充,它允許它處於調試模式(盡管技術上UB否則)並且可能通過保護字節寫入。
同樣,它在malloc代碼上崩潰,因為你使用的是sizeof(node)
vs sizeof(Node)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.