What is MPI_Free_mem?

MPI library

MPI (Message Passing Interface) is a library that enables you to write parallel programs in C or Fortran77. The library uses commonly available operating system services to create parallel processes and exchange information among these processes.

Parallel processing

MPI_Free_mem

This method frees the memory allocated by MPI_Alloc_mem.

Syntax

int MPI_Free_mem(void *baseptr)

Parameters

  • baseptr is the pointer to the beginning of the memory segment allocated by MPI_Alloc_mem.

Return value

The function returns an error if it is unsuccessful. By default, the error aborts the MPI job.

In case of success, it returns MPI_SUCCESS, the value returned upon successful termination of any MPI routine.

Example

The example below shows how to free up allocated memory using the MPI_Free_mem method in C:

#include "mpi.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
    int *ptr;

    MPI_Init(&argc, &argv);
    MPI_Alloc_mem(100*sizeof(int), MPI_INFO_NULL, &ptr);
    MPI_Free_mem(ptr);
    MPI_Finalize();
    return 0;
}

We first allocate memory using MPI_Alloc_mem with ptr representing the beginning of the memory block. We then free up the allotted space using the MPI_Free_mem function.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved