我正在尝试实现van emde boas树,但是当我两次以上地调用我的插入函数时,我得到了一个分段错误11。当我尝试打印insert函数中的值时,我意识到问题出在我将集群初始化为NULL时。我不明白为什么当我使用for循环时会出现这种情况。我也尝试使用calloc,但它给了我相同的错误。我的代码如下所示:
class vEB
{
int u;
int *m;
int *M;
vEB *summary;
vEB **cluster;
public:
vEB(int);
bool member(int);
void insert(int);
void Delete(int);
int min();
int max();
int* pred(int);
int* succ(int);
};
vEB::vEB(int u){
this -> u = u;
this -> m = NULL;
this -> M = NULL;
if (u == 2){
this -> summary = NULL;
this -> cluster = NULL;
} else {
int subSize = (int)sqrt(u);
this -> summary = new vEB(subSize);
this -> cluster = new vEB*[subSize];
for (int i=0;i<=subSize;i++){
cluster[i]=NULL;
}
}
}
bool vEB::member(int x){
if (u == 2){
if (m == NULL){
return false;
}
if (x == 0){
return ((*m) == 0);
} else if (x == 1){
return ((*M) == 1);
}
return false;
}else{
if (m == NULL) {
return false;
}
if (x < (*m) || x > (*M)){
return false;
}else if (x == (*m) || (x == (*M))){
return true;
}else{
int subSize = (int)sqrt(u);
int hi = x / subSize;
int lo = x % subSize;
if (cluster[hi] == NULL){
return false;
} else{
return cluster[hi] -> member(lo);
}
}
}
}
void vEB::insert(int x) {
if (u == 2) {
if (x == 0) {
if (m == NULL){
m = new int;
M = new int;
(*m) = (*M) = x;
} else {
(*m) = x;
}
} else if (x == 1) {
if (M == NULL){
m = new int;
M = new int;
(*m) = (*M) = x;
} else{
(*M) = x;
}
}
} else {
if (m == NULL) {
m = new int;
M = new int;
(*m) = (*M) = x;
} else {
if (x < (*m)) {
int currMin = (*m);
(*m) = x;
this -> insert(currMin);
}else {
int subSize = (int)sqrt(u);
int hi = x / subSize;
printf("%d - %d\n",x, hi);
int lo = x % subSize;
printf("%d - %d\n",x, hi);
if (cluster[hi] == NULL){
cluster[hi] = new vEB(subSize);
cluster[hi] -> insert(lo);
summary -> insert(hi);
}else {
cluster[hi] -> insert(lo);
}
if (x > (*M)){
(*M) = x;
}
}
}
}
}
void vEB::Delete(int x){
if (u == 2) {
if (x == 0) {
if ((*M) == 0){
m = M = NULL;
} else{
(*m) = 1;
}
} else if (x == 1) {
if ((*m) == 1) {
m = M = NULL;
}
else {
(*M) = 0;
}
}
}else{
int subSize = (int)sqrt(u);
int hi = x / subSize;
int lo = x % subSize;
if (x == (*m)){
if (x == (*M)){
m = M = NULL;
} else {
int nextMinHi = summary -> min();
int nextMinLo = cluster[summary -> min()] -> min();
int nextMin = nextMinHi * subSize + nextMinLo;
this -> Delete(nextMin);
(*m) = nextMin;
}
} else {
cluster[hi] -> Delete(lo);
if (cluster[hi] -> m == NULL){
summary -> Delete(hi);
delete cluster[hi];
cluster[hi] = NULL;
}
if (x == (*M)){
if (summary -> m == NULL) {
(*M) = (*m);
} else{
int nextMaxHi = summary -> max();
int nextMaxLo = cluster[summary -> max()] -> max();
(*M) = nextMaxHi * subSize + nextMaxLo;
}
}
}
}
}
int vEB::min() {
return (*m);
}
int vEB::max() {
return (*M);
}
int* vEB::pred(int x){
if (u == 2){
if (x == 0) {
return NULL;
} else if (x == 1){
if (m == NULL){
return NULL;
}
if ((*m) == 1){
return NULL;
}
return m;
}
else {
return NULL;
}
} else {
if (m == NULL) {
return NULL;
}
if (x <= (*m)) {
return NULL;
}
if (x > (*M)) {
return M;
}
int subSize = (int)sqrt(u);
int hi = x / subSize;
int lo = x % subSize;
if (cluster[hi] == NULL){
int* prev = summary -> pred(hi);
int* ret = new int;
(*ret) = (*prev) * subSize + cluster[(*prev)] -> max();
return ret;
} else {
int *newLo, *newHi;
newHi = new int;
newLo = new int;
(*newHi) = hi;
int minInCluster = cluster[hi] -> min();
if (lo > minInCluster){
newLo = cluster[hi] -> pred(lo);
}else {
newHi = summary -> pred(hi);
(*newLo) = cluster[(*newHi)] -> max();
}
int *ret = new int;
(*ret) = (*newHi) * subSize + (*newLo);
return ret;
}
}
}
int* vEB::succ(int x) {
if (u == 2) {
if (x == 1) {
return NULL;
}else if (x == 0) {
if (M == NULL) {
return NULL;
}
if ((*M) == 0) {
return NULL;
}
return M;
}else {
return NULL;
}
}else{
if (m == NULL) {
return NULL;
}
if (x >= (*M)) {
return NULL;
}
if (x < (*m)) {
return m;
}
int subSize = (int)sqrt(u);
int hi = x / subSize;
int lo = x % subSize;
if (cluster[hi] == NULL) {
int* next = summary -> succ(hi);
int* ret = new int;
(*ret) = (*next) * subSize + cluster[(*next)] -> min();
return ret;
} else {
int *newLo, *newHi;
newHi = new int;
newLo = new int;
(*newHi) = hi;
int maxInCluster = cluster[hi] -> max();
if (lo < maxInCluster){
newLo = cluster[hi] -> succ(lo);
}else {
newHi = summary -> succ(hi);
(*newLo) = cluster[(*newHi)] -> min();
}
int *ret = new int;
(*ret) = (*newHi) * subSize + (*newLo);
return ret;
}
}
}
int main(){
vEB *vEB = new class vEB(8);
vEB -> insert(1);
vEB -> insert(2);
vEB -> insert(5);
vEB -> insert(6);
vEB -> insert(7);
printf("%d\n", (*vEB -> pred(2)));
printf("%d\n", (*vEB -> succ(2)));
vEB -> Delete(2);
return 0;
}有没有合适的方法来初始化一个我不知道的指针?任何建议都将不胜感激。谢谢。
发布于 2019-03-28 22:42:13
在vEB::vEB(int u)中
(int i=0;i<=subSize;i++)的
{ clusteri=NULL;}
必须是
(int i=0;i { clusteri=NULL;}
因为this -> cluster = new vEB*[subSize];而不是this -> cluster = new vEB*[subSize+1];
当我调用我的插入函数超过两次时,我得到了一个分段错误11。
当您执行vEB -> insert(5);时,hi的值太高,并且cluster[hi]不在集群中。
发布于 2019-03-28 22:47:52
虽然off-by-one错误很糟糕,但您有一个更糟糕的错误:可能的无限递归。
在你的VEC构造函数中,你可以检查u == 2,但是如果u是3,会发生什么呢?然后你将进入else的情况,你得到了3的平方根,它将被截断为整数1。然后执行new VEC(1),它使用值1 (不等于2)调用构造函数,进入else分支并获得等于1的1的平方根。然后执行new VEC(1) (再次执行!)以此类推,直到永远。
您需要检查是否为u <= 2以避免这种情况。
发布于 2019-03-29 00:13:31
这不是你的问题的解决方案,但我希望它能有所帮助。通过一些调试,我发现您的问题出在调用insert(5)时。产生segfault的代码是:
if (cluster[hi] == NULL) {
cluster[hi] = new vEB(subSize);
cluster[hi]->insert(lo);
summary->insert(hi);
}
else {
cluster[hi]->insert(lo); //here something is wrong with cluster[hi]
}我找不到答案是什么,但是如果你把你的代码放在IDE中并调试它,你就有更好的机会解决这个问题,因为你比我更了解你的代码。
https://stackoverflow.com/questions/55400154
复制相似问题