c语言创建线程段违例,段错误(段违例)在 key = *(q+last);出现的

段异常(段违例)在 key = *(q+last);出现的

代码

#include 

#include 

int partition(char *s,int begin,int last);

void Quicksort(char *s1,int first,int end);

int main(){

char array[]={6,4,1,2,3};

Quicksort(array,0,4);

printf("the sorted array is :\n");

printf("%s\n",array);

system("pause");

}

void Quicksort(char *s,int first,int end){

int p;

if (first 

p = partition(s,first,end);

Quicksort(s,first,p-1);

Quicksort(s,p+1,end);

}

}

int partition(char *s,int begin,int last){

int i,j,p;

char key,value;

char *q;

q = (char*)malloc(sizeof(char)*(last - begin+1));

i = 0;

p= i -1;

for(i = begin;i 

if (*(q+i) <= key){

p ++;

value = *(q+i);

*(q+i) = *(q+p);

*(q+p) = value;

}

}

*(q+last) = *(q+1+p);

*(q+1+p) = key;

for(j = begin;j <= last;j++)

*(s+j) = *(q+j);

}

各位大神帮忙看看。。。

------解决方案--------------------

简直就是乱七八糟啊,

你的partition函数和s数组有毛关系?

------解决方案--------------------

http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express

点开Visual C++ 2010 Express下面的语言选‘简体中文’,再点立即安装

再参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\qsort.c

------解决方案--------------------

30 i = 0;

31 p= i -1;

37 *(q+p) = value;

p始终是-1,这样(q+p) == q-1

于是就引用了不属于自己的内存范围,出现segmentation violation。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值