简体   繁体   English

程序出现SIGSIGV错误

[英]Program giving SIGSIGV Error

I have written following C++ program which is giving me 我写了下面的C ++程序,它给了我

SIGSIGV Error for very large input. SIGSIGV错误,输入很大。

Basically the following program outputs Number smaller than current number in an array.I have use BST and array to implement. 基本上下面的程序输出的Number小于数组中的当前数字。我已经使用BST和array来实现。

#include <iostream>
using namespace std;

struct bst {
    long long int data;
    struct bst *right;
    struct bst *left;
};

struct bst *head=NULL;
//function to increment 
void increment(struct bst *node,long long int w[]) {
    if(node==NULL)
    return;
    w[node->data] += 1;
    increment(node->left,w);
    increment(node->right,w);
}

void insert(long long int data,long long int w[]){
    struct bst *node;
    struct bst *current;
    current=head;
    if(head==NULL) {
        node = new bst;
        node->data = data;
        node->right = NULL;
        node->left=NULL;
        head=node;
        }else {
        while(1) {
            if(data > current->data){
                if(current->right!=NULL)
                current=current->right;
                else {
                    node = new bst;
                    node->data = data;
                    node->right = NULL;
                    node->left=NULL;
                    current->right=node;
                    break;
                }
            }
            else{
                if(current->left!=NULL){
                    //if data is less than current number than incremet 1 in right sub tree of current node 
                    //and current node
                    w[current->data] +=1;
                    increment(current->right,w);
                    current=current->left;

                }
                else {
                    node = new bst;
                    node->data = data;
                    node->right = NULL;
                    node->left=NULL;
                    current->left=node;
                    //if data is less than current number than incremet 1 in right sub tree of current node 
                    //and current node
                    w[current->data] +=1;
                    increment(current->right,w);
                    break;
                }
            }

        }
    }
}

void deletenode(struct bst *node) {
    if(node == NULL)
    return;
    deletenode(node->left);
    deletenode(node->right);
    delete(node);
}


int main() {
    int t;
    cin>>t;
    while(t--) {
        long long int n;long long int counter=0;
        cin>>n;
        long long int a[n];
        //array to keep count of numbers which are less than index of w[i]
        long long int w[1000000]={0};
        for(long long int i=0;i<n;i++) {
            cin>>a[i];
            insert(a[i],w);
        }
        for(long long int i=0;i<n;i++) {
            cout<<w[a[i]]<<" ";
        }
        cout<<"n";
        //deleting BST
        deletenode(head);
        head=NULL;
    }
    return 0;
}

Any help appreciated. 任何帮助表示赞赏。

In main this may be causing problem - main ,这可能会造成问题-

long long int w[1000000]={0};

You should allocate memory on heap . 您应该在堆上分配内存。 You can do something like this - 你可以做这样的事情-

long long int *w=new long long int[1000000]

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

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