我找到了python的答案,但我不明白.
代码是修改后的合并排序.对于我检查到的少量输入,它工作得很好.但是当我通过在线判断运行时,当输入数量很高(500)时,它给了我这个错误:
Error in 'a.out': corrupted size vs. prev_size: 0x0000000000d5b8b0
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f3b83a5b7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x80dfb)[0x7f3b83a64dfb]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f3b83a6853c]
a.out[0x4009d1]
a.out[0x400ac7]
a.out[0x400a87]
a.out[0x400aa4]
a.out[0x400a87]
a.out[0x400bc7]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f3b83a04830]
a.out[0x4005b9]
======= Memory map: ========
Run Code Online (Sandbox Code Playgroud)
它还有15行.为什么我收到此错误?是因为我在使用动态分配内存时遇到的一些错误malloc?
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
void *Merge(int *A,int l,int m,int r,int *B,int *F);
void *Merge(int *A,int l,int m,int r,int *B,int *F){
int i=l,j=m,k=0,*C,x,y=l,z,cSize,temp,*D,*E;
cSize = r-l;
C = (int *) malloc (cSize * sizeof(int));
D = (int *) malloc (cSize * sizeof(int));
E = (int *) malloc (cSize * sizeof(int));
while (k < cSize){
if((j==r) || ((i!=m) && ((A[j]*B[i]) >= (A[i]*B[j])))){
C[k] = A[i];
D[k] = B[i];
E[k] = F[i];
i++;
k++;
}
if((i>=m) || ((j!=r) && ((A[j]*B[i]) < (A[i]*B[j])))){
C[k] = A[j];
D[k] = B[j];
E[k] = F[j];
j++;
k++;
}
}
for(x=0;x<k;x++){
A[y] = C[x];
B[y] = D[x];
F[y] = E[x];
y++;
}
free(C);
free(D);
free(E);
}
void *MergeSort(int *A,int left,int right,int *B,int *C);
void *MergeSort(int *A,int left,int right,int *B,int *C){
int mid,i,j,k=0,l=0,*R,*L;
if(right - left == 1){
A[left] = A[left];
}
if(right-left > 1){
mid = (left+right)/2;
MergeSort(A,left,mid,B,C);
MergeSort(A,mid,right,B,C);
Merge(A,left,mid,right,B,C);
}
}
int main(){
int n,i=0,newNumt,newNumo,*a,*b,*c;
scanf("%d",&n);
a = (int *) malloc (n * sizeof(int));
b = (int *) malloc (n * sizeof(int));
c = (int *) malloc (n * sizeof(int));
for(i=0;i<n;i++){
scanf("%d %d",&a[i],&b[i]);
c[i]= i+1;
}
MergeSort(a,0,n,b,c);
for(i=0;i<n;i++){
printf("%d\n",c[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是一篇旧帖子,但有几个问题似乎尚未解决,因此以下尝试解决您在帖子中特别提到的一些问题。
正如评论中提到的,导致您的主要问题的本质是通过-Wall在编译期间进行设置找到的,然后通过调试器运行它,并在提示符下输入最多 20 组整数对。
下面是经过一些修改的完整代码。有些只是建议,但有些是对编译警告的响应,有些比其他更重要。这些都是评论的。
解决您的主要问题之一:为什么我会收到此错误?是因为我在使用 malloc 动态分配内存时犯了一些错误吗?:
正如我的@Jonathan Leffler 提到的,这不太可能是内存分配的问题,而是尝试访问尚未分配的内存的问题。
与此相关的值得注意的运行时错误是在运行未修改的代码时出现的,并在函数中用注释标记Merge(),其中索引k递增到大于应有的值,导致对越界指针的取消引用错误。快速解决方法是通过在第二部分中添加 来使两个if部分互斥。else此修改确实可以防止运行时错误,但不一定是正确的(或唯一的)所需的更改。
我没有在代码中提到的另一项建议是变量名称的选择。正如所写的,许多内容过于神秘,并且不会增加代码尝试执行的操作的可读性或理解性。建议使用变量名称,以便其他人(甚至是您自己,当 3 年后您再次查看此内容时)立即了解该变量的用途。
阅读更改的评论以了解它们为何存在......
#include <stdio.h>
#include <stdlib.h>
void *Merge(int *A,int l,int m,int r,int *B,int *F);
void *MergeSort(int *A,int left,int right,int *B,int *C);
int main()
{
// int n,i=0,newNumt,newNumo,*a,*b,*c;
int n,i=0,*a,*b,*c; //removed unused newNumt & newNumo
printf("\nEnter number of integer pairs:\n");//user input instructions
scanf("%d",&n);
a = calloc (n, sizeof(int));//see comment in MergeSort for similar
b = calloc (n, sizeof(int));//suggested change for malloc/calloc
c = calloc (n, sizeof(int));
for(i=0;i<n;i++)
{
printf("\n%d) Enter two integer values:\n", i);//user input instructions
scanf("%d %d",&a[i],&b[i]);
c[i]= i+1;
}
MergeSort(a,0,n,b,c);
for(i=0;i<n;i++)
{
printf("%d\n",c[i]);
}
return 0;
}
void *Merge(int *A, int l, int m, int r, int *B, int *F)
{
//int i=l,j=m,k=0,*C,x,y=l,z,cSize,temp,*D,*E;
int i=l,j=m,k=0,*C,x,y=l,cSize,*D,*E;//removed unused z and temp
cSize = r-l;
// C = (int *) malloc (cSize * sizeof(int));
// D = (int *) malloc (cSize * sizeof(int));
// E = (int *) malloc (cSize * sizeof(int));
C = calloc (cSize, sizeof(int)); //it is not recommended to cast the return
D = calloc (cSize, sizeof(int)); //of malloc/calloc/realloc in C
E = calloc (cSize, sizeof(int)); //changed malloc to calloc to initialize
//variable memory to zero before use
// Only one or the other of the following two if statements should be executed per loop,
// by running both an access violation occurs causing crash. (eg. when k is incremented twice
// before being tested.)
while (k < cSize)//k is tested only once per loop...
{
if((j==r) || ((i!=m) && ((A[j]*B[i]) >= (A[i]*B[j]))))
{
C[k] = A[i];
D[k] = B[i];
E[k] = F[i];
i++;
k++;//if k == csize-1, it will be incremented to k == csize, then go into the next section
}
else if((i>=m) || ((j!=r) && ((A[j]*B[i]) < (A[i]*B[j])))) //added else
{
C[k] = A[j]; //Dereference of out-of-bounds pointer occurs here when k is too large.
D[k] = B[j];
E[k] = F[j];
j++;
k++;// ... but possibly increment twice!
}
}
for(x=0;x<k;x++)
{
A[y] = C[x];
B[y] = D[x];
F[y] = E[x];
y++;
}
free(C);
free(D);
free(E);
return 0; //function prototype requires a return to quiet the warnings
//Only void function prototypes do not require a return statement
}
void *MergeSort(int *A,int left,int right,int *B,int *C)
{
//int mid,i,j,k=0,l=0,*R,*L;
int mid = 0; //removed all unused variables and initialized mid
if(right - left == 1)
{
A[left] = A[left];
}
if(right - left > 1)
{
mid = (left + right)/2; // integer rounding
MergeSort(A, left, mid, B, C);
MergeSort(A, mid, right, B, C);
Merge(A, left, mid, right, B, C);
}
return 0; //function prototype requires a return to quiet the warnings
//Only void function prototypes do not require a return statement
}
Run Code Online (Sandbox Code Playgroud)