# Is it possible to assign a pointer to a variable?

**URL:** https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343
**Category:** General Discussion
**Created:** [November 22, 2021, 8:56am UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343 "2021-11-22T08:56:34Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)
#### Post date: [November 22, 2021, 8:56am UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/1 "2021-11-22T08:56:34Z")

</div>

Hello,

I was wondering if it is possible to assign a pointer to a variable like in C++ for instance, in order to access the value stored by this variable using (\*).

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [November 22, 2021, 12:40pm UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/2 "2021-11-22T12:40:03Z")

</div>

Why would you want to do that? Do you have a specific scenario in mind?

---

<div class="post-metadata">

### Author: ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)
#### Post date: [November 22, 2021, 1:31pm UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/3 "2021-11-22T13:31:47Z")

</div>

I have a set of functions that I have converted from c++ to FreeFem++ format and some of them have arguments assigned by a unary operator (\*). So I am having trouble in passing arguments to these types of functions in FreeFem++ especially functions that have MPI\_Comm as argument.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [November 22, 2021, 1:34pm UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/4 "2021-11-22T13:34:34Z")

</div>

Couldn’t you pass references instead of addresses? Could you post a small code snippet?

---

<div class="post-metadata">

### Author: ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)
#### Post date: [November 22, 2021, 1:56pm UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/5 "2021-11-22T13:56:05Z")

</div>

This is one of the functions that I defined:

long CwipiInitFfpp(MPI\_Comm const &common\_comm, string \*const &application\_name, MPI\_Comm \*const &application\_comm){  
cwipi\_init( cwipi\_init, (const char \*) application\_name, application\_comm);  
return 0L;  
}

In FreeFem++ I use It as follows (just to test the arguments):  
string test = “test”;  
mpiComm Comm(mpiCommWorld, 0, 0);  
mpiComm Comm2(mpiCommWorld, 0, 0);  
CwipiInitFfpp(Comm, test, Comm2);

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [November 22, 2021, 1:59pm UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/6 "2021-11-22T13:59:45Z")

</div>

What is wrong with this code and/or what would you want to do instead?

---

<div class="post-metadata">

### Author: ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)
#### Post date: [November 22, 2021, 2:03pm UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/7 "2021-11-22T14:03:02Z")

</div>

The problem is when passing MPI arguments FreeFem++ gives a segmentation fault error, I checked both the cpp file and the edp file and they do have the same compiler, I thought maybe it is because of the pointer type FreeFemm++ doesn’t understand it. Maybe the MPI library used in FreeFem++ is different from the one used in CWIPI? they are both used in C++ though

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [November 22, 2021, 2:18pm UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/8 "2021-11-22T14:18:09Z")

</div>

Could you please give a backtrace of the segmentation fault?

---

<div class="post-metadata">

### Author: ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)
#### Post date: [November 22, 2021, 2:23pm UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/9 "2021-11-22T14:23:11Z")

</div>

17 : CwipiInitFfpp(Comm, test, Comm2)[0]PETSC ERROR: ------------------------------------------------------------------------

[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range

[0]PETSC ERROR: Try option -start\_in\_debugger or -on\_error\_attach\_debugger

[0]PETSC ERROR: or see [https://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind](https://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind)

[0]PETSC ERROR: or try [http://valgrind.org](http://valgrind.org) on GNU/linux and Apple Mac OS X to find memory corruption errors

[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run

[0]PETSC ERROR: to get more information on the crash.

[0]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------

[0]PETSC ERROR: Signal received

[0]PETSC ERROR: See [https://www.mcs.anl.gov/petsc/documentation/faq.html](https://www.mcs.anl.gov/petsc/documentation/faq.html) for trouble shooting.

[0]PETSC ERROR: Petsc Release Version 3.15.0, Mar 30, 2021

[0]PETSC ERROR: /home/cfd/mnoun/freefem/4.9/bin/FreeFem+±mpi on a named krakenv6 by mnoun Mon Nov 22 15:20:51 2021

[0]PETSC ERROR: Configure options --prefix=/home/cfd/mnoun/freefem/4.9/ff-petsc/r MAKEFLAGS= --with-debugging=0 COPTFLAGS="-O3 -mtune=generic" CXXOPTFLAGS="-O3 -mtune=generic" FOPTFLAGS="-O3 -mtune=generic" --with-cxx-dialect=C++11 --with-ssl=0 --with-x=0 --with-fortran-bindings=0 --with-cudac=0 --with-cc=/softs/intel/impi/2018.1.163/bin64/mpicc --with-cxx=/softs/intel/impi/2018.1.163/bin64/mpicxx --with-fc=/softs/intel/impi/2018.1.163/bin64/mpif90 --with-scalar-type=real --with-blaslapack-include= --with-blaslapack-lib="-llapack -lblas" --download-metis --download-ptscotch --download-hypre --download-parmetis --download-mmg --download-parmmg --download-superlu --download-suitesparse --download-tetgen --download-slepc --download-hpddm --download-scalapack --download-mumps --download-slepc-configure-arguments=–download-arpack=https://github.com/prj-/arpack-ng/archive/b64dccb.tar.gz PETSC\_ARCH=fr

[0]PETSC ERROR: #1 User provided function() at unknown file:0

[0]PETSC ERROR: Run with -malloc\_debug to check if memory corruption is causing the crash.

application called MPI\_Abort(MPI\_COMM\_WORLD, 50176059) - process 0

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [November 22, 2021, 2:25pm UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/10 "2021-11-22T14:25:58Z")

</div>

This is the PETSc backtrace, which is not very meaningful here. Need to run with `-start_in_debugger` and then use GDB to get the backtrace of your plugin.

---

<div class="post-metadata">

### Author: ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)
#### Post date: [November 23, 2021, 10:01am UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/11 "2021-11-23T10:01:13Z")

</div>

I think I figured out what the problem is, I am passing an mpiComm argument to my function that takes an MPI\_Comm argument. I tried declaring the original function in a function that takes a string as argument only and it worked:

long CwipiInitFfpp(string \*const &application\_name){  
MPI\_Comm temp;  
MPI\_Comm\_dup(MPI\_COMM\_WORLD, &temp);  
cwipi\_init( MPI\_COMM\_WORLD, (const char \*) application\_name, (&temp) );  
return 0L;

This bieng said is the mpiComm in FreeFemm++ different from the MPI\_Comm in C++ ?

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [November 23, 2021, 11:57am UTC](https://community.freefem.org/t/is-it-possible-to-assign-a-pointer-to-a-variable/1343/12 "2021-11-23T11:57:27Z")

</div>

It shouldn’t be different, but FreeFEM pass a reference, and it looks like your function `cwipi_init` may overwrite it. You can look, for example, at `src/mpi/parallelempi.cpp`, function `mpiCommSplit`, to do what you want, I think.
