程序给出SIGSIGV错误
Program giving SIGSIGV Error
我写了下面的c++程序,它给了我
SIGSIGV输入太大错误。
基本上,下面的程序输出比数组中当前数字小的数字。我使用BST和数组来实现。
#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;
}
感谢您的帮助。
在main
中这可能会导致问题-
long long int w[1000000]={0};
应该在堆上分配内存。你可以这样做-
long long int *w=new long long int[1000000]
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 程序给出SIGSIGV错误