0

I am trying to call malloc again after initializing another dynamically allocated array, but my program fails to run (though it can pass the compilation). Part of my code is as follows.

table = (Node **)malloc(m * sizeof(Node*));

for(i=0; i<=m; i++)
  table[i] = NULL;

table2 = (Node *)malloc(n * sizeof(Node));

The error information is like:

malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)
->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_si
ze == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (st
ruct 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 weirdest thing is that I have found that my program can run successfully after removing the second & third lines in my code above, in which NULL is assigned to table[i]. I am a little confused because I don't know what causes this malloc error. In addition, is it proper to assign NULL to newly allocated pointers?

Thanks!

9
  • 3
    Are the pointers returned by malloc valid? Show all relevant code. Which part of the assertion actually fails? Commented Jul 4, 2015 at 2:30
  • 5
    i<=m will access unallocated memory when i == m. Commented Jul 4, 2015 at 2:32
  • 3
    If that is C code: do not cast void * as returned by malloc! Remove the C++-tag these are different languages, you would not add Java either just because they same some syntax. Commented Jul 4, 2015 at 2:32
  • 1
    I've never heard of the language "C/C++" is it new? Commented Jul 4, 2015 at 2:40
  • 2
    You're welcome. valgrind would have told you about it. Commented Jul 4, 2015 at 2:46

2 Answers 2

5

Isn't i<=m in the for loop going to go outside the region you allocated in the first malloc() call? You allocated m Node pointers in your table, and then set m+1 entries equal to NULL.

Sign up to request clarification or add additional context in comments.

7 Comments

It is and presents our good ol' "friend" undefined behaviour.
But the erros seems to pop up in malloc itself. Ok, "do not expect anything after UB"
Are you talking to me? I'm not OP!
@MarkZ: No need to explain to me. I did noticed already. You should still get your code in shape; I did not comment just for fun! For the message, I agree. That is quite a beast of an expression. Sorry, took me some time to realize it was not from your code.
Just reinforcing that once you step off the end of that array and corrupt memory, you can't really depend on the behavior or the error messages. Likely you trashed something malloc was using/depending on. Some allocates keep the user data and (internal allocation) list data together.
|
1
for(i=0; i<=m; i++)
  table[i] = NULL;

The second expression needs to be changed to i < m. You've allocated m slots. The range of access is 0...(m-1)

table, size = 3

+---+---+---+
| 0 | 1 | 2 |
+---+---+---+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.