C Sysmalloc assertion failure

℡╲_俬逩灬. 提交于 2019-12-13 07:11:07

问题


I am getting the below sysmalloc error in running a C program.

malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *)
&((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd))))
&& old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)
((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))
+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1)))
&& ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.

The program works fine when using an int array

int(*M)[cnt] = malloc(sizeof(int[cnt][cnt]));

but is showing the above error for signed long int. There is no other change made in the program.

signed long int(*M)[cnt] = malloc(sizeof(signed long int[cnt][cnt])); 

What could be the reason? This worked perfectly when using an int array. Hence there shouldn't be a problem with memory management as given here C Malloc assertion failure

Thanks


回答1:


This assertion expression looks like a sanity check to see if the allocation internal data structures are still intact.

This internal data is often placed before and/or after the allocated blocks. If something is wrong, it means that your code that executed before this malloc() has been writing outside the bounds of an earlier allocated block.

EDIT: Doing a google search for Assertion (old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - directly led me to this. Didn't you do a google search?




回答2:


My own experience, which might help: This is definitely a failure because of some dynamically allocated memory which is used incorrectly (for example, out of bound assignment). The tricky thing is that the failure might not occur in exactly the line where the out of bounds is happening.

For example, this was my code:

int func(const int n) {
    int *a = new int[n]; // * sizeof(int)
    for (int i = 0; i < n; ++i)
        a[i] = 1;

    // This line causes the sysmalloc assertion failure
    vector<int> tv(10);
}

Now the failure happens when memory is allocated for tv, but the actual problem is that I forgot the * sizeof(int) while allocating memory and hence in the loop, wrote in non allocated memory addresses.



来源:https://stackoverflow.com/questions/31936229/c-sysmalloc-assertion-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!