简体   繁体   English

输入数组后程序挂起

[英]program hangs after inputting array

#include<stdio.h>
#include<conio.h>
//#include<alloc.h>

int* mergeSort(int*,int);
int* merge(int*,int*,int);

void main()
{
  int n;int i=0;
  int *a,*b;

  scanf("%d",&n);
  a=(int)malloc(n*sizeof(int));
  for(;i<n;i++)
     scanf("%d",&a[i]);

  b=mergeSort(a,n);
  for(i=0;i<n;i++)
     printf("%d ",b[i]);

}

int* mergeSort(int *b,int n)
{
  int temp,*s;

  if(n>2)
  {
     mergeSort(b,n/2);
     mergeSort(b+n/2,n-n/2);
     s=merge(b,b+n/2,n);

     return s;
  }
  else if(n==2)
  {
     if(b[0]>b[1])
     {
         temp=b[0];
         b[0]=b[1];
         b[1]=temp; 
     }
     return;
  }
}

int* merge(int* a,int* c,int n)
{
  int i=0,j=0,k=0,
  int* x;

  while( (j ! =n/2) && (k != (n-n/2)) && (i < n))
  {
       if(a[j]<c[k])
       {
             x[i]=a[j];
             j++; 
             i++;
       }
       else
       {
             x[i]=c[k];
             k++;
             i++;
       }
   }
   for( ; j<n/2; j++,i++)
      x[i]=a[j];

   for( ; k < (n-n/2); k++,i++)
      x[i]=c[k];

   return x;
}

when i run this code,it hangs after inputting all the elements of the array in first for loop. 当我运行此代码时,它在第一个for循环中输入数组的所有元素后挂起。 Please help me, how can i correct it to make it work successfully? 请帮助我,我该如何纠正它才能成功工作? it hangs on calling the mergeSort function from main() function. 它挂在从main()函数调用mergeSort函数上。

it hangs after inputting all the elements of the array in first for loop.

hangs ? Are you sure... that's pretty good considering your merge code declares a pointer to an int: 您确定吗...考虑到您的合并代码声明了一个指向int的指针,这非常好:

int *x;

and never initializes it, then tries to jump to an offset ( i ) past it: 并从不初始化它,然后尝试跳转到它之后的偏移量( i ):

x[i]=a[j];

Change your code to this: 将代码更改为此:

int *x = malloc(n * sizeof(int));

and it should stop crashing/hanging whatever. 并且它应该停止崩溃/挂起任何东西。

FYI, whenever you malloc() you should free() right now you have memory leaks. 仅供参考,每当您使用malloc()都应该立即free()内存泄漏。

There are some things wrong, not only basic things like void main instead of int main but also eg the return; 有些事情是错误的,不仅是基本的事情,例如void main而不是int main ,还包括例如return; in mergeSort() which should return int* and the uninitialized x . mergeSort() ,它应该返回int*和未初始化的x Also one important thing about the algorithm: You seem to assume that n is a power of 2: you recursively divide by 2 and assume that it always is an untruncated integer. 关于该算法的另一件重要事情是:您似乎假设n是2的幂,您递归除以2,并假定它始终是一个不被截断的整数。

For starters, the code doesn't compile in a C++ compiler (I know it's C code, but still...), 对于初学者来说,该代码无法在C ++编译器中编译(我知道它是C代码,但仍然...),

a couple of problems are: 有几个问题是:

line 11: 第11行:

a=(int)malloc(n*sizeof(int));

why are you casting a pointer to an int? 为什么要投射一个指向int的指针? a is an int * a是一个整数*

line 38: 第38行:

return;

you are returning from mergeSort without a return value...that can't be good 您正在从mergeSort返回而没有返回值...那可能不是很好

I recommend that you fix all the compiler errors and warnings, then try again 我建议您修复所有编译器错误和警告,然后重试。

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

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