免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3514 | 回复: 1
打印 上一主题 下一主题

在Ubuntu下运行会段错误,Win下可以正常运行,为什么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-22 23:41 |显示全部楼层 |倒序浏览
以下是我数据结构课程中的一个作业.我在Ubuntu下用g++编译后,运行会显示"段错误",但是在Win下用g++编译后,运行没有提示.我不知道我哪里出问题了.用gdb调试,程序似乎是在其中的一个FilterDown函数中中断的.但是我看不出问题.诸位帮忙看看.


  1. #include<iostream>
  2. using namespace std;


  3. class heap{
  4. public:
  5.   heap(int size = 10):maxsize(size) , currentsize(0) { heapArray = new int[10]; }
  6.   heap(int a[] , int size);  //通过一个数组构建一个堆.
  7.   ~heap(){delete [] heapArray;}
  8.   int getTop(){return heapArray[0];}
  9.   bool deletetop();  //删除堆顶
  10.   bool insert(int data); //插入一个元素
  11.   bool isempty() const{ return currentsize == 0; }
  12.   bool isfull() const{ return currentsize == maxsize; }
  13.   int sizeofheap() { return currentsize; }
  14.   void setempty(){ currentsize = 0 ;}
  15.   

  16. private:
  17.   int *heapArray;
  18.   int currentsize;
  19.   int maxsize;
  20.   void FilterDown(int p); //向下调整函数
  21.   void FilterUp(int p );  //想上调整函数



  22. };

  23. bool heap::deletetop()
  24. {
  25. if ( currentsize == 0 )
  26.   return false;         
  27. heapArray[0] = heapArray[currentsize-1];
  28. currentsize--;
  29. FilterDown(0);
  30. return true;

  31. }

  32. bool heap::insert(int data)
  33. {
  34. if ( currentsize == maxsize )
  35.   return false;

  36. heapArray[currentsize] = data;
  37. FilterUp(currentsize);
  38. currentsize++;
  39. return true;
  40. }

  41. void heap::FilterUp(int p)
  42. {
  43. int temp;
  44. int i = p;
  45. int j = (i - 1) / 2;
  46. while( j >= 0 )
  47.   {
  48.     if ( heapArray[i] < heapArray[j] )
  49.      {
  50.        temp = heapArray[i];
  51.        heapArray[i] = heapArray[j];
  52.        heapArray[j] = temp;
  53.      }
  54.     else
  55.      break;

  56.     i = j;
  57.     j = (j - 1) / 2;
  58.   }
  59. }

  60. heap::heap(int a[] , int size)
  61. {
  62.   maxsize = size + 10;
  63.   heapArray = new int[maxsize];
  64.   heapArray = a;
  65.   currentsize = size;
  66.   int i = (currentsize - 2) / 2;
  67.   while(i >= 0)
  68.    {
  69.      FilterDown(i--);
  70.    }
  71. }



  72. void heap::FilterDown(int p)
  73. {
  74.   int i = p;
  75.   int j = i * 2 + 1;
  76.   int temp;
  77.   while ( j < currentsize )
  78.    {
  79.       
  80.      if ( ( (j + 1) < currentsize ) && ( heapArray[j] > heapArray[j+1] ) )
  81.        j++;
  82.    
  83.      if ( heapArray[i] > heapArray[j] )
  84.       {
  85.         temp = heapArray[i];
  86.         heapArray[i] = heapArray[j];
  87.         heapArray[j] = temp;
  88.       }  
  89.      else
  90.       break;

  91.      i = j;
  92.      j = j * 2 + 1;

  93.    }     



  94. }

  95. int main()
  96. {
  97.   int a[] = {80 , 57 , 99 , 35 , 23 , 11 , 74 , 29 , 62 , 16};
  98.   heap b(a , 10);
  99.   b.insert(14);
  100.   b.deletetop();

  101. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2006-12-23 13:09 |显示全部楼层
谢谢,谢谢。我不应该直接指向那个数组,应该复制一下。呵呵

指针看来是很危险的东西!

[ 本帖最后由 janusle 于 2006-12-23 14:12 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP