简体   繁体   English

使用malloc的C语言中的错误:malloc.c:2451:sYSMALLOc“断言失败”

[英]Error in C using malloc: malloc.c:2451:sYSMALLOc “Assertion Failed”

I'm a beginner in C and am having memory allocation problems. 我是C语言的初学者,遇到内存分配问题。 I checked the related discussions. 我检查了相关的讨论。 I should probably use Valgrind, but till the time I learn how to use that, I'm posting the problem here. 我可能应该使用Valgrind,但是直到我学会如何使用它之前,我都会在这里发布问题。

Here's a link to a Merge Sort code I made. 这是我制作的“合并排序”代码的链接。 http://ideone.com/utEzoq http://ideone.com/utEzoq

However, the main problem seems to be in the following section: 但是,主要问题似乎在以下部分中:

void main()
{
     MergeSort(list, 0, n-1) //calling function on pointer to array of integers
}

int *MergeSort(int *A, int x, int y) //declaration
{
if(x==y)
{
    return A;
}

else
{
    int size=1+y-x;
    int half=(x+y)/2;

    MergeSort(A, x, half);  
    MergeSort(A, half+1, y);    


    int *C;
    C=(int *)malloc(size*sizeof(int));
    int j=x;
    int k=half;
    int i=0;


    while((j<=half)||(k<=y))            
    {
        if(A[j]<=A[k])
        {
            C[i]=A[j];
            j++;
        }
        else
        {
            C[i]=A[k];
            k++;
        }
        i++;
    }


    if(j==(half+1))
    {
        while(i<size)
        {
            C[i]=A[k];
            i++;
            k++;
        }
    }
    else if(k==(y+1))
    {
        while(i<size)
        {
            C[i]=A[j];
            i++;
            j++;
        }
    }

    return C;
}

The error however arises with different kinds of inputs. 但是,错误是由不同种类的输入引起的。 When I entered a reverse sorted and sorted array, it returned the output in the order of input. 当我输入反向排序的数组时,它按输入顺序返回了输出。 And random numbers give the malloc "Assertion Failed" error. 随机数会导致malloc“ Assertion Failed”错误。

Help would be much appreciated. 帮助将不胜感激。

You write to elements of C that are beyond the length of the array in your first while loop, thereby probably corrupting information that malloc and free use internally. 您在第一个while循环中写入了超出数组长度的C元素,从而可能破坏了内部mallocfree使用的信息。

The whole implementation has errors. 整个实现都有错误。 For example, you call the sorting function recursively and allocate auxiliary memory with every call. 例如,您递归调用排序函数,并在每次调用时分配辅助内存。 You return the allocated buffer, but never do anything with it, let alone free it. 您返回分配的缓冲区,但是永远不要对其进行任何操作,更不用说free它了。 The sorting function doesn't sort, because the sorted data is (supposedly) in C , which you ignore. 排序功能不会排序,因为排序后的数据(据说)在C ,您可以忽略。 You effectively print out A . 您有效地打印了A

Edit: There's no need to learn Valgrind. 编辑:没有必要学习 Valgrind。 Install it, compile your program with -g , then run your program with Valgrind. 安装它,使用-g编译程序,然后使用Valgrind运行程序。 In addition to your output, you'll get error and warning messages that state clearly where what kind of memory violation occurs. 除了输出之外,您还将收到错误和警告消息,清楚地指出发生哪种类型的内存冲突。 Install Valgrind now and make a habit of using it - it will save you time in the future. 立即安装Valgrind并养成使用它的习惯-它将在以后节省您的时间。

your problem 你的问题

void main()

to

int main()

,

int k=half;

to

int k=half+1;

,

while((j<=half)||(k<=y)) 

to

while((j<=half)&&(k<=y))

,

return C;

to

for(i=0;i<size;++i){
    A[x+i]=C[i];
}
free(C);
return A;

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

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