Take the 2-minute tour ×

I am having a new little problem;

I have a little pointer called:

int *a;

Now ..somewhere inside my main method I allocate some space for it using the following lines and assign a value:

a = (int *) malloc(sizeof(int));

..and then I attempt to transmit it (say to process 1):

MPI_Bsend(a, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);

On the other end, if I try to receive that pointer

int *b;
MPI_Recv(b, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

printf("This is what I received: %d \n", *b);

I get an error about the buffer!

However if instead of declaring 'b' a pointer I do the following:

int b;
MPI_Recv(&b, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

printf("This is what I received: %d \n", b);

...all seems to be good! Could someone help me figure out what's happening and how to only use pointer?

Thanks in advance!

share|improve this question

3 Answers 3

up vote 5 down vote accepted

The meaning of the line


is the following: "a is a point in memory where I have 1 integer. Send it.`

In the code you posted above, this is absolutely true: a does point to an integer, and so it is sent. This is why you can receive it using your second method, since the meaning of the line


is "receive 1 integer, and store it at &b. b is a regular int, so it's all good. In the first receive, you're trying to receive an integer into an int* variable there is no allocated memory that b is pointing to, so Recv has nowhere to write to. However, I should point out:

NEVER pass a pointer's contents to another process in MPI

MPI processes cannot read each others' memory, and virtual addressing makes one process' pointer completely meaningless to another.

share|improve this answer
Thanks! It's all clear now. –  Konos5 Mar 26 '12 at 18:32
I think the real issue here is that no memory was allocated for int *b! Also, MPI takes care of transmitting data from one process to another if required, so that one process does not have to be able to read the memory of the other! –  mort Mar 26 '12 at 18:43
True, the real problem was the fact that b was unallocated. It's still a fairly moot point though, as pointers themselves should not be passed in any case. –  suszterpatt Mar 26 '12 at 19:33

This problem is related to handling pointers and allocating memory; it's not an MPI specific issue.

In your second variant, int a automatically allocates memory for one integer. By passing &a you are passing a pointer to an allocated memory segment. In your first variant, memory for the pointer is automatically allocated, but NOT for the memory the pointer is pointing to. Thus, when you pass in the pointer, MPI tries to write to non-allocated memory, which causes the error.

It would work this way though:

int *b = (int *) malloc(sizeof(int));
share|improve this answer

The error you are getting is that you are copying a result from MPI_Recv in some memory *b that you don't own and isn't initialised.

Not an expert on MPI but surely you can't transfer a pointer (ie a memory address) to a process that could be running on another machine!

share|improve this answer
+1 for your first sentence, -1 for the second though: The pointers used for the MPI_Send and MPI_Recv functions are used as read/write buffers by MPI. MPI will take care of transferring the data in the write buffer of one process to the read buffer of the other. –  mort Mar 26 '12 at 18:34
@mort: as you said, it is the buffer contents that get transferred. Transferring the pointer (ie. the address) to another machine can be done of course, but is meaningless. So Martin's answer is correct. –  Greg Inozemtsev Mar 26 '12 at 19:48

Your Answer


