# Saving multiple solutions

Hi,
when there is an array of solutions, say two of them called sols, allocated with

MMG5_pSol sols[2];
int types[2] = {MMG5_Scalar, MMG5_Scalar};
MMG2D_Set_solsAtVerticesSize(mesh2, sols, 2, n, types);

where n is the number of vertices of the mesh, the command

MMG2D_saveAllSols(mesh2, sols, “sols.sol”);

works and saves both correctly. Saving just the first one with

MMG2D_saveSol(mesh, sols[0], “firstone.sol”);

works and saves just the first one out of the two but

MMG2D_saveSol(mesh, sols[1], “secondone.sol”);

segfaults instead of saving the second one.
It seems that the saveSol command somehow makes the implicit assumption that the MMG5_pSol argument does not come out of an array of solutions. I guess that’s not the intended behaviour?
I’d debug that myself but it seems this is about some internal assumption that is not so easy to see.
Cheers, Mathias

Hi Mathias,

the `MMG2D_Set_solsAtVerticesSize` function must allocate the array of solution for you (so you don’t have to take care of our internal structures). The following code should work:

``````MMG5_pSol sols=NULL;
int types[2] = {MMG5_Scalar, MMG5_Scalar};
MMG2D_Set_solsAtVerticesSize(mesh2, &sols, 2, n, types);
``````

Then the saveSol function should work if you call it without dereferencing the solutions:

``````MMG2D_saveSol(mesh, sols+0, “firstone.sol”);
MMG2D_saveSol(mesh, sols+1, “secondone.sol”);
``````

The error comes from the fact that a MMG5_pSol is a pointer over a solution structure (MMG5_Sol). Thus, `MMG5_pSol sols[2]` declares an array of 2 pointers over solution structures while internally, Mmg would have allocated an array of solutions (instead of pointers) and expects a pointer over this array.
To summarize : the C language manages differently a pointer over an array than an array of pointer.

Just one final remark: these arrays of solutions are not used by the main Mmg library functions (remeshing, ls discretization…), the API functions to set and get them are provided for convenience only…

Regards,

Algiane