0

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

Community
  • 1
  • 1
re3el
  • 735
  • 2
  • 12
  • 28
  • 1
    What is `cnt`? Are you on a 32 or 64 bit system? What is `sizeof(int)` and `sizeof(long)` on your system? – mch Aug 11 '15 at 08:03
  • possible duplicate of [Why do I get a C malloc assertion failure?](http://stackoverflow.com/questions/2987207/why-do-i-get-a-c-malloc-assertion-failure) – meaning-matters Aug 11 '15 at 08:17
  • @mch: cnt is an integer variable storing some value. I am on 64 bit system. `sizeof(int)` is 4 and `sizeof(long)` is 8. – re3el Aug 11 '15 at 08:27
  • What the typical size of `cnt`? – meaning-matters Aug 11 '15 at 08:37
  • @meaning-matters: In the particular case that I am working on, it is somewhere around 30 – re3el Aug 11 '15 at 08:38
  • have you tried `void *tmp = malloc(cnt * cnt * sizeof(long));`? Your syntax is a little bit uncommon. – mch Aug 11 '15 at 08:43
  • @mch: I did know that, but the above used one is the best suitable method in my case. – re3el Aug 11 '15 at 08:44

2 Answers2

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?

Community
  • 1
  • 1
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
  • yup. I searched and checked that answer. That didn't help my problem. If it was the case mentioned in the other answer it shouldn't work for the int array as well. – re3el Aug 11 '15 at 08:24
  • 1
    Not a good conclusion because issues like this can appear and disappear depending on where allocated blocks are placed, or how far they stretch. The programming mistake can be in a totally different and seemingly unrelated place, but the fact is that your code is writing where it's not supposed to. – meaning-matters Aug 11 '15 at 08:44
0

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.

forumulator
  • 836
  • 12
  • 12