简体   繁体   English

日食中的CDT问题

[英]Issue with CDT in eclipse

I am using eclipse for C++ program. 我正在使用eclipse进行C ++程序。

After building my code when I am running the code I am getting name.exe has stopped working error. 在我运行代码时构建我的代码后,我得到name.exe已停止工作错误。 Same code is working fine here http://codepad.org/2c5xFbLM . 相同的代码在http://codepad.org/2c5xFbLM正常工作。

Please help me finding this issue. 请帮我找到这个问题。
Thanks in advance. 提前致谢。
My code : 我的代码:

#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;
}

Since you're using C++, use the new operator instead of malloc , the crash happened upon the 14th iteration at your call to malloc, or your 4th node creation, because of the additional padding with debugging, it allowed it in debug mode ( though technically UB otherwise ) and probably wrote through the guard bytes. 由于您使用的是C ++,因此使用new运算符而不是malloc ,崩溃发生在您调用malloc或第4个节点创建的第14次迭代时,因为通过调试进行额外的填充,它允许它处于调试模式(尽管技术上UB否则)并且可能通过保护字节写入。

Likewise, it crashed on the malloc code because you were using sizeof(node) vs sizeof(Node) . 同样,它在malloc代码上崩溃,因为你使用的是sizeof(node) vs sizeof(Node)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM