-1

I have the following code, I need to free an array of the d_array structure after using it and I get always SiGABRT.

typedef struct d_array
{
  int len;
  double * content;
} d_array;


quantile(d_array arr,  d_array percentages, d_array borders )
{
  int i;

  d_array temp_arr;

  temp_arr.content =(double *) malloc(sizeof(double)*arr.len);
  memcpy(temp_arr.content, arr.content, sizeof(double)*arr.len);

  qsort(temp_arr.content, arr.len, sizeof(double), d_compare);

  double med = arr.content[(int)(arr.len/2)];

  for(i=0; i< percentages.len; i++)
   {
    borders.content[i] = temp_arr.content[(int)(100*(percentages.content[i]))];
   }

  free(temp_arr.content);

  }
Marouf Mim
  • 51
  • 1
  • 4
  • You need to make a "destructor" function (and you should make some "constructor" function as well). You might want (with C99) instead to use a flexible array member like [here](http://stackoverflow.com/a/23433573/841108) – Basile Starynkevitch Sep 02 '14 at 08:12
  • Why you use a new array just for a double value? Why not use double *temp? Easier and less clutter. – Igor Pejic Sep 02 '14 at 08:14
  • @Igor: because he wants to keep the array size. And you mean `double`, not `float` !! – Basile Starynkevitch Sep 02 '14 at 08:15
  • @BasileStarynkevitch What size? Also `arr.len = arr.len;` line does nothing. – Igor Pejic Sep 02 '14 at 08:18
  • 1
    Your code could be improved a bit (e.g., declare `quantile()` as `void`, remove the redundant statement `arr.len = arr.len;`, spell "percentage" properly), but I can't see any memory leaks here. I think your problem lies elsewhere. Also, why is this question tagged as [tag:segmentation-fault]? – r3mainer Sep 02 '14 at 08:19
  • 1
    What does arr.len in this line do? Why is it needed? `memcpy(temp_arr.content, arr.content, sizeof(double)*arr.len);` – Igor Pejic Sep 02 '14 at 08:21
  • I am sorry, I know it has some mistakes It is not finished yet , I have to add interpolation function to it, but the problem is that I cannot free the memory allocated to the content field of the structure. – Marouf Mim Sep 02 '14 at 08:38
  • @Igor arr.len is length of array //items number// , pozdrav druze ;) – Marouf Mim Sep 02 '14 at 09:02
  • Pozdrav :) Yeah, I know, but I don't think that multiplying with arr.len is needed in that case. If you could include your main function it would be great, because it would help in debugging. – Igor Pejic Sep 02 '14 at 09:07
  • Meanwhile, have a look at http://stackoverflow.com/q/605845/3194340 and http://stackoverflow.com/q/4931123/3194340 – n0p Sep 02 '14 at 09:47

2 Answers2

0

Based on the tagging "Segmentation-fault", I believe you are facing a crash of the program with signal "segmentation-fault". You might need to check the value of percentages.content[i] in the below code. The 100 * that value might be going beyond what was allocated for temp_arr.content.

borders.content[i] = temp_arr.content[(int)(100*(pecentages.content[i]))];

As far as memory leak, i dont find anything in this function.

  • you are right, the problem is SIGABRT not memory leak it is my fault. But it still does not work. Consider it is borders.content[5]=1; – Marouf Mim Sep 02 '14 at 09:00
  • Please add a check before borders.content[i] assignment like below: `if((100 * pecentages.content[i]) > arr.len) { printf ("Array size exceeded\n"); return; }` and let me know what happens. – Shankar Narayanan Sep 02 '14 at 11:16
0

Here is my function finished but the free is working now because I added struct before the declaration of temp_arr: This could be helpful for other people maybe:

 void quantile(d_array arr,  d_array percentages, d_array borders ) 
 {
 int i;
 struct d_array temp_arr;

 temp_arr.content = (double *)malloc(sizeof(double)*(arr.len+2));
 memcpy(temp_arr.content+1, arr.content, sizeof(double)*arr.len);
 t_d(temp_arr.content, 25);
 qsort(temp_arr.content+1, arr.len, sizeof(double), d_compare);
 t_d(temp_arr.content, 25);

 temp_arr.content[0] = temp_arr.content[1];
 temp_arr.content[arr.len+1] = temp_arr.content[arr.len];
 t_d(temp_arr.content, 25);


 d_array q;
 q.len = arr.len+2;
 q.content = malloc(sizeof(double)*(arr.len+2));

 q.content[0] = 0;
 q.content[1] = 100*0.5/arr.len;
 q.content[arr.len+1] = 100;

 for(i=2; i<arr.len+1 ;i++)
   q.content[i] = (q.content[i-1] + (double)100/arr.len);

 int indx=0;
 double elem=0;

 for(i=0; i< percentages.len; i++)
 {

   find(q, percentages.content[i]*100, '>', &indx,  &elem);

   if (temp_arr.content[indx] == temp_arr.content[indx-1] )
     borders.content[i] = temp_arr.content[indx-1];
   else
     borders.content[i] = temp_arr.content[indx-1] + (percentages.content[i]*100 -    q.content[indx-1])*(temp_arr.content[indx]-temp_arr.content[indx-1])/(q.content[indx]-q.content[indx-1]) ;

 }
         free(temp_arr.content);
   }
find function:
int find(d_array mat, double el, char op, int *indx, double * eee)
{
  int i;
  if (op=='>')
   for(i=0;i<mat.len ;i++)
    if (mat.content[i]>el)
       {
        *indx = i;
        *eee= mat.content[i];
        return 0;
       }
  else if(op=='=')
   for(i=0;i<mat.len ;i++)
    if (mat.content[i] == el)
       {
        *indx = i;
        *eee = mat.content[i];
        return 0;
       }
  else if(op=='<')
   for(i=0;i<mat.len ;i++)
    if (mat.content[i] < el)
       {
        *indx = i;
        *eee = mat.content[i];
        return 0;
       }
   return 1;

}

and: struct d_array { double *content int len } d_array;

the call is : double test_sig[] = {1.0, 0.0, 2.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 15.0, 0.0,51.0, 20.0, 514.0, 2.0, 0.0, 151.0, 20.0, 51.0, 0.0, 016.0, 05.0}; struct d_array pecentages; pecentages.content=malloc(sizeof(double)*5); pecentages.len=5;

      struct d_array  borders;
      borders.content=malloc(sizeof(double)*5);
      borders.len=5;

      mat.content = test_sig;
      pecentages.content[0] = 0.1;
      pecentages.content[1] = 0.2;
      pecentages.content[2] = 0.5;
      pecentages.content[3] = 0.8;
      pecentages.content[4] = 0.9;
      quantile( mat, pecentages, borders );
Marouf Mim
  • 51
  • 1
  • 4