Bonjour, j´ai un probleme qui j´en suis quasiment sure vient de la conversion d´entier en double avec l´utilisation des fonctions malloc , realloc ou alors mxMalloc , mxRealloc . J´utilise des fonctions dans une librarie creer par matlab qui prennent en entree des doubles. J´utilise les fonctions malloc , realloc et mxMalloc , mxRealloc dans mon programme. Tout marche bien quand je travaille qu´avec des doubles mais lorsque je recupere des entiers d´une application, que je les convertis en double et qu´ensuite je les passe en entrees dans une fonction de la librairie matlab ca marche quelques iterations et apres j´ai un message d´erreur´qui me dit qu´il y a une violation d´acces par rapport a libmx.dll.

N´y a t-il pas des problemes lorsqu´on utilise les fonctions malloc, realloc, mxMalloc ou mxRealloc .

ci dessous abstract de mon program :
le program est compose de deux boucles if(Why=0) et if(Why=1).
On parcour la Why=0 une seule fois et on parcours plusieurs fois Why=1
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
 #include<stdio.h>
#include<stdlib.h>
#include <vcl.h>

#pragma hdrstop
#include "OptPanel.h"
#include <stdlib.h>
#include "global.h"

#include "algorithms/david_algorithm/david_algorithm.h"

#include "libfinal.h"



long david_algorithm(FILE *out, int Why, long *Answer, tParams Parameters[], int *CollisionFlag)
{
    MainForm->MemoInfo->Lines->Add("beginning1 test just to see...");
    AnsiString Message;


    static mxArray *in1=NULL, *in2=NULL, *in3=NULL, *in4=NULL, *in5=NULL, *in6=NULL, *in7=NULL, *in8=NULL, *in9=NULL, *in10=NULL, *in12=NULL;
    static mxArray *out1 = NULL;
    static mxArray *out2 = NULL;
    static mxArray *out3 = NULL;
    static mxArray *out4 = NULL;
    static mxArray *out5 = NULL;
    static mxArray *out6 = NULL;
    static mxArray *out7 = NULL;
    static mxArray *out8 = NULL;
    static mxArray *out9 = NULL;
    static mxArray *out10 = NULL;


    static int* tab1;
    static int* tab3;
    static int* tab2;

    static double* tab11;
    static double* tab22;
    static double* tab23;

    static double N=MainForm->EditParameter2->Text.ToInt(), pdim=10, n1=0; 
    static int N1=MainForm->EditParameter2->Text.ToInt();
    static int i1=0,i2=1, i3=0,i4=0,i5=0, i6=0,i7=0, i8=0,i9=0,i10=0,i11=0,i12=0, i13=0, i14=0, i15=0, i20=0,i21=0;

    static double* data2;


    static int data111[NUMBER_OF_PARAMS];
    static int data222[NUMBER_OF_PARAMS];
    static double data1111[10];
    static double data2222[10];
    static double data5[] = {1};
    static double data6[1];

    
    static double data9[] = {10};
    static double data10[] = {N};
    static double data12[1];
    typedef double Coordonnees [10];
    static Coordonnees * tab4 =(Coordonnees*)malloc(N * sizeof *tab4 );


    if(Why==0)
    {
      MainForm->MemoInfo->Lines->Add("David algorithm init why == 0 ...");
      if( !mclInitializeApplication(NULL,0) )
    {
        fprintf(out, "Could not initialize the application.\n");
        MainForm->MemoInfo->Lines->Add("David algorithm couldn´t initialise the application");


    	return -1;
    }

     if (!libfinalInitialize())
    {
        fprintf(out,"Could not initialize the library.\n");
         MainForm->MemoInfo->Lines->Add("David algorithm couldn´t initialise the library");
        return -2;
    }


    n1 = N1 * pdim;
    data6[0]= i2;
    tab1=(int*)malloc(pdim * sizeof(int)); 
    tab2=(int*)malloc(n1 * sizeof(int)); 
    tab3=(int*)malloc(pdim * sizeof(int));

   
    tab11=(double*)malloc(pdim * sizeof(double)); 
    tab22=(double*)malloc(n1 * sizeof(double)); 
    tab23=(double*)malloc(n1 * sizeof(double));
     
    data2=(double*)malloc(N1 * sizeof(double));// to create the results of the simulation for 25 points


      for (i4=0; i4<NUMBER_OF_PARAMS; i4++)
        {
                         
                        if(glParam[i4].Active)
                        {

                        data111[i4] = Parameters[i4].Low;
                        data222[i4] = Parameters[i4].High;

                        }
                        else
                        {
                        data111[i4] = -1;
                        data222[i4] = -1;

                        }
                        if (i4>=0 && i4<NUMBER_OF_PARAMS && data111[i4]!=-1 && data222[i4]!=-1)
                        {
                        data1111[i5]=data111[i4];
                        data2222[i5]=data222[i4];
                         i5++;
                        }

       }



    in1=(mxArray*)mxMalloc(N1*pdim * 64 ); // 64 bytes = 8 octets = 1 double
    in2=(mxArray*)mxMalloc(N1 * 64);


    in2 = mxCreateDoubleMatrix(1,N1,mxREAL);
    in3 = mxCreateDoubleMatrix(1,pdim,mxREAL);
    in4 = mxCreateDoubleMatrix(1,pdim,mxREAL);
    in5 = mxCreateDoubleMatrix(1,1,mxREAL);
    in6 = mxCreateDoubleMatrix(1,1,mxREAL);

    in7 = mxCreateDoubleMatrix(1,pdim,mxREAL);
    in8 = mxCreateDoubleMatrix(1,pdim,mxREAL);
    in9 = mxCreateDoubleMatrix(1,1,mxREAL);
    in10 = mxCreateDoubleMatrix(1,1,mxREAL);


     //on copie ds la partie reelle de tous les elements de  in1 les double de data1
     //mxGetPr return the address of the first element of the real data
    memcpy(mxGetPr(in3), data1111, pdim*sizeof(double));
    memcpy(mxGetPr(in4), data2222, pdim*sizeof(double));
    memcpy(mxGetPr(in5), data5, 1*sizeof(double));  
    memcpy(mxGetPr(in6), data6, 1*sizeof(double));


    memcpy(mxGetPr(in7), data1111, pdim*sizeof(double));
    memcpy(mxGetPr(in8), data2222, pdim*sizeof(double));
    memcpy(mxGetPr(in9), data9, 1*sizeof(double));
    memcpy(mxGetPr(in10), data10, 1*sizeof(double));


       mlfInitdoe(1, &out10, in9, in10, in7, in8);//first function de la librairie 
       display(out10,tab2,tab22);//recupere le resultat en entier tab 2 ou en double tab 22
       mxDestroyArray(out10), out10=0;

       
        for(i1=0; i1<N1; i1++)
                {                    
                        // coordinate of the points to evaluate for initialization
                          for(i3=0; i3<NUMBER_OF_PARAMS; i3++)
                                {
                                        fprintf(out,"\n");
                                        if(glParam[i3].Active)
                                        {//debut de if
                                        Parameters[i3].CurrentValue = tab2[i1+i14*N1];
                                        i14++;
                                        }
                                        else
                                        {
                                        Parameters[i3].CurrentValue = Parameters[i3].Default;
                                        }

                                 }
                                                     
                                 
                i14=0;
                }   // enf of the loop with i1

   
                for(i8=0; i8<N1; i8++)
                {
                data2[i8]= pdim * (i8+1);  //represent les valeurs retourne de la simulation pour lobjective function
                }
                memcpy(mxGetPr(in2), data2, N1*sizeof(double));

            
          for(i9=0; i9<pdim ;i9++)
          {

                for(i6=0; i6<N1; i6++)
                {

                int k2;
                k2=N1*i9+i6;
                tab4[i6][i9] = tab22[k2];//tab22 contient les coordoonees en double de N1 points en pdim dimension
                tab23[k2]=tab22[k2];
                tab4[i6][i9] = tab2[k2]; //tab2 contient la meme chose que tab22 mais en entier
                tab23[k2]=tab2[k2];
                
                i10++;
                 }
          }
         
        in1 = mxCreateDoubleMatrix(N1,pdim,mxREAL);
        memcpy(mxGetPr(in1), tab23, N1*pdim*sizeof(double)); 

         mlfNewpoint(9, &out1, &out2, &out3, &out4, &out5, &out6, &out7, &out8, &out9, in1, in2, in3, in4, in5, in6);//second method of the library
         display(out1,tab1,tab11);// recupere les coordonnees dun point en entier tab1 et en double tab11

                               
          return MainForm->EditParameter1->Text.ToIntDef(1);
         }// end of the  why=0

         if(Why==1)
         {
           Message.sprintf(" next point number : %d \n",i13);
          

           data2=(double*)realloc(data2, (N+i13+1) * sizeof(double));
           i2=i13+2;
           data6[0]= i2;
           in12 = mxCreateDoubleMatrix(1,1,mxREAL);
           memcpy(mxGetPr(in12), data6, 1*sizeof(double));

           i15=0;

           for(i3=0; i3<NUMBER_OF_PARAMS; i3++)
                                {
                                 if(glParam[i3].Active)
                                   {//debut de if
                                   Parameters[i3].CurrentValue = tab1[i15];
                                   i15++;
                                   }
                                   else
                                   {
                                    Parameters[i3].CurrentValue = Parameters[i3].Default;
                                   }
                                                                 }

           data2[N1+i13]=100;

           
         in1=(mxArray*)mxRealloc(in1,((N1+i13+1)*pdim)*sizeof(double));
         in2=(mxArray*)mxRealloc(in2,(i13+1+N1)*sizeof(double));

         in1 = mxCreateDoubleMatrix((N1+i13+1),pdim,mxREAL);
         in2 = mxCreateDoubleMatrix(1,N1+i13+1,mxREAL);





          tab4=(Coordonnees*)realloc(tab4, (N1+i13+1) * sizeof(Coordonnees));
          tab22=(double*)realloc(tab22,(N1+i13+1)*pdim *sizeof(double));
          tab23=(double*)realloc(tab23,(N1+i13+1)*pdim * sizeof(double));


                 for(i11=0; i11<pdim; i11++)
                 {
                tab4[N1+i13][i11]=tab11[i11];//tab11 contient les coordonnees double en pdim dimension
                 tab4[N1+i13][i11]=tab1[i11];//tab1 contient les coordonne entieres du point 
                 }




          for(i12=0;i12<pdim;i12++)
          {
                for(i7=0; i7<N1+i13+1; i7++)
                {
                int k1=(N1+i13+1)*i12+i7;
                tab22[k1]=tab4[i7][i12]; //je rempli tab22 avec tous les elements double dont j´ai besoin pour utiliser la fonction newpoint
                tab23[k1]=tab4[i7][i12];// je rempli tab23 avec tous les elements entiers
                }
          }
         memcpy(mxGetPr(in1), tab22, (N1+i13+1) * pdim * sizeof(double));
         memcpy(mxGetPr(in1), tab23, (N1+i13+1) * pdim * sizeof(double));
         memcpy(mxGetPr(in2), data2, (N1+i13+1) * sizeof(double));


         mlfNewpoint(9, &out1, &out2, &out3, &out4, &out5, &out6, &out7, &out8, &out9, in1, in2, in3, in4, in5, in12);
         display(out1,tab1,tab11);



        mxDestroyArray(out1); out1=0;
        mxDestroyArray(out2); out2=0;
        mxDestroyArray(out3); out3=0;
        mxDestroyArray(out4); out4=0;
        mxDestroyArray(out5); out5=0;
        mxDestroyArray(out6); out6=0;
        mxDestroyArray(out7); out7=0;
        mxDestroyArray(out8); out8=0;
        mxDestroyArray(out9), out9=0;

             

        fprintf(out,"test fin sur %d \n",i13);
        i13++;

    
     if(i13>MainForm->EditParameter1->Text.ToInt())
     {

        mxDestroyArray(in1); in1 = 0;
        mxDestroyArray(in2); in2 = 0;
        mxDestroyArray(in3); in3 = 0;
        mxDestroyArray(in4); in4 = 0;
        mxDestroyArray(in5); in5 = 0;
        mxDestroyArray(in6); in6 = 0;
        mxDestroyArray(in7); in7 = 0;
        mxDestroyArray(in8); in8 = 0;
        mxDestroyArray(in9); in9 = 0;
        mxDestroyArray(in10); in10 = 0;
        mxDestroyArray(in12); in12 = 0;

        MainForm->MemoInfo->Lines->Add("outside the why==1");

         MainForm->MemoInfo->Lines->Add("David algorithm finish");

         libfinalTerminate();

         mclTerminateApplication();


        return -1;  // Stop signal
     }
    else
     {

        return 1; // refaire une loop why=1
     }



    }//End of why = 1




} // end of the long




void display(const mxArray* in, int* tab, double* tabdoub)
{
      int i=0, j=0; /* loop index variables */
      int r=0, c=0; /* variables to store the row and column length of the matrix */
      int k=0;
      double *data; /* variable to point to the double data stored within the mxArray */

      r = mxGetM(in); //number of rows
      c = mxGetN(in); //number of columns

      data = mxGetPr(in); //Get real data elements in mxArray

      for( i = 0; i < r; i++ )
      {

           for( j = 0; j < c; j++)
              {

              k=10*i+j;
              tab[k] = (data[i*c+j]+0.5);         // good conversion between integer and double
              tabdoub[k] =  data[i*c+j];
              }
      }

}
En gras les parties ou je pense avoir un probleme. En effet lorsque j´ai les parties en gras (tout en double), je n´ai aucun probleme tout marche. MAis avec les parties en gras rouge (conversion integer en double) j´ai un probleme alors que je converti juste des entiers en double.En faite le programme semble marcher correctement (les valeurs renvoyes sont les bonnes) mais il plante au bout de plusieurs boucle why=1. Je suis conscient que les lignes de codes sont nombreuses c´est pour ca que j´ai mis les endroits important en gras. Dites moi si vous avez besoin de precision sur quelque chose.