簡體   English   中英

使用 malloc 的 C 中的錯誤:大小損壞與 prev_size

[英]error in C using malloc : corrupted size vs prev_size

我找到了 python 的答案,但我不明白。

代碼是修改后的歸並排序。 對於我檢查到 10 的少量輸入,它工作正常。但是當我通過在線判斷運行它時,當輸入數量很高(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: ========

它還有另外 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;
}

這是一篇舊帖子,但有幾個問題似乎沒有得到解決,因此以下嘗試解決您在帖子中特別提到的一些問題。

正如評論中提到的,通過在編譯期間設置-Wall ,然后通過調試器運行它,在提示符下輸入多達 20 組整數對,可以找到導致您的主要陳述問題的問題的性質。

以下是經過多次修改的完整代碼。 有些只是建議,但有些是對編譯警告的回應,有些比其他更重要。 這些都是有評論的。

解決您的主要問題之一:為什么我會收到此錯誤? 是不是因為我在使用 malloc 動態分配內存時犯了一些錯誤?
正如我的@Jonathan Leffler 所提到的,這不太可能是內存分配的問題,而是嘗試訪問尚未分配的內存。
與此相關的值得注意的運行時錯誤在運行未修改的代碼時出現,並在Merge()函數中用注釋標記,其中索引k增加到大於它應有的值,導致取消引用 out-越界指針錯誤。 快速解決方法是通過在第二個if部分添加一個else來使兩個if部分互斥。 這種修改確實可以防止運行時錯誤,但不一定是所需的正確(或唯一)更改。

另外一個建議,我並沒有在代碼地址是一組變量的選擇。 正如所寫的那樣,很多都太神秘了,並沒有增加代碼試圖做什么的可讀性或理解性。 建議是使用變量名,讓另一個人(甚至是你自己,當 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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM