A SSCCE will be very helpful.
However, I will try to answer as best as possible:
I have a malloc'd integer array that I populate with MPI_Recv
MPI_Recv(d.current, n, MPI_INT, 0, TAG_CURRENT_ARRAY, MPI_COMM_WORLD, &status);
How big is the array? How exactly did you do this? What is n in this case and how is it related to the size of malloc() ed?
Your observations indicate that the cause of this error is MPI_Recv() . To make this error, MPI_Recv() wrote outside the memory area malloc() ed, which it is not allowed. This will spoil either the linked list used internally by memory management or the size of the blocks behind it or both, resulting in the indicated error.
I tested the d.current value both before and after MPI_Recv, and it does not change (this is correct).
(How to do this? You pass a pointer to a function, not its address, so the pointer cannot change.)
However, if I try to free the data, I get an error message:
* Error in `./bin/obddhe-mpi ': free (): invalid next size (fast): 0x0965e988 *
The same thing as before the reception works great.
This is another hint for what I wrote above: the meory behind the block you are using has been freed and contains a pointer to the next free area. If you free() your memory, the library tries to combine the free blocks, the second of which is damaged, which leads to this error.
Imagine that you have the following situation:
- The memory manager adds each block of memory, whether free or allocated, with its length.
- Free blocks have the address of the next free block from the very beginning - this is the linked list that I mentioned.
- Your highlighted block added with its length is followed
- a free block added with its length and containing the address of the next free block NULL if there is no next free block.
Then, if you sign up for the end of a block of memory, the length and contents of the next block will be affected and changed.
It doesnβt affect anything - until now.
But if you call free() on your block, that block will be merged with the free block after it.
To do this, the following steps must be performed:
- Move the linked list to find adjacent free blocks, which may already lead to this error, because the βnextβ pointer of the second free block is garbage.
- Calculate the size of a larger free block from other blocks. If one of them contains trash, the trash will be used to calculate a new, larger free block and perfect confusion.