c - glibc error detected no idea what is wrong -


#include<stdio.h> #include<stdlib.h> #include<math.h> //#include<mathscall.h> #include<time.h> #include"plot.h"  int main() {     int no_packets = 10000;     //float lamda = 0.1;     double lamda = 0.1,*lamdas;     int i,j,cumulative_x,count = 0;     int trans_time = 1;     double temp,*throughput;     int iterations = 1/0.1;     printf("iterations: %d\n",iterations);     throughput = (float *)malloc(iterations*sizeof(float));     lamdas = (float *)malloc(iterations*sizeof(float));         int *pkt_collision;     double *pkt_holder;     int k=0;     float p;     for(p=0.1;p<1.1;p=p+0.1)      {         lamdas[k]=p;          printf("lamdas: %f\n",lamdas[k]);          k++;         //printf("k: %d\n",k);      }     int l=0;      while(lamda<1.1)     {          count = 0;          temp = lamdas[l] * no_packets;          int avg_packets = (int)temp;          //printf("in here\n");          pkt_holder = (double *)malloc(avg_packets*sizeof(double));          pkt_collision = (int *)malloc(avg_packets*sizeof(int));          //temp = lamda * no_packets;          //printf("in here\n");          srand(0);           for(i=0;i<avg_packets;i++)          {             pkt_holder[i] = 0.0;             pkt_collision[i] = 0;          }          for(i=1;i<avg_packets;i++)         {                double x;                             {                 x= (rand() % 10000) / 10000.0;                }while(x == 0);                    double time_temp = -(double)log(x)/lamda;              //printf("i: %d\t time_temp: %f\n ",i,time_temp);               pkt_holder[i]=pkt_holder[i-1] + time_temp;  }    for(i=1;i<avg_packets;i++) {              for(j=i;j<avg_packets;j++)              {                 if((pkt_holder[j]-pkt_holder[i-1])<=5 &&pkt_collision[j]==1)                 {                       pkt_collision[j] = 1;                      pkt_collision[i-1] = 1;                  }                  else                     break;             }         }  for(i=0;i<avg_packets;i++) {              if(pkt_collision[i] == 0)               {                  count++;              }  }         throughput[l] = (float) count/no_packets;          lamda+=0.1;     free(pkt_holder);         free(pkt_collision);// glibc error occuring after execution of //statement         l++;  }    printf("sucess: %d\n",count);  return 0; } 

in program trying simulate throughput of pure aloha.

beginning lamda 0.1 lamda = 1, trying find throughput corresponding these values.

in while loop trying reallocate size of both pkt_holder , pkt_collision in each iteration freeing memory @ ending of each iteration of loop.

when tried debug in eclipse showing glibc error when free(pkt_collision) executed.

any or advice appreciated.

thank you

i suspect overwriting in following sequence:

lamdas = (float *)malloc(iterations*sizeof(float));     int *pkt_collision; double *pkt_holder; int k=0; float p; for(p=0.1;p<1.1;p=p+0.1)  {     lamdas[k]=p;      printf("lamdas: %f\n",lamdas[k]);      k++;     //printf("k: %d\n",k);  } 

you allocate iterations slots lamdas, when iterate, don't iterate integer index, floating point index. might 1 more iteration think (this has inability represent 0.1 in floating point).

the solution write

for(int k = 0; k < iterations; k++) {   lamdas[k] = 0.1 * (k+1);   // etc } 

since pointer *pkt_collision stored right after lamdas, , since compilers store information required free right before pointer (this not standard, experience), writing past end of allocated space lamdas does, in fact, write space used free.

i think solve it.

edit noticed declaring lamdas double*, casting pointer float*. didn't see compiler warning?

always enable compiler warnings, , heed them

so have 2 problems. need allocate enough memory:

lamdas = malloc(iterations * sizeof(double)); 

and use integer loop variable.

as aside, can prevent problem in future writing

lamdas = malloc(iterations * sizeof(*lamdas)); 

this way don't have remember type - compiler know, , right.

update compiling code gcc -wall gives:

temp.c:16:16: warning: incompatible pointer types assigning 'double *'       'float *' [-wincompatible-pointer-types]     throughput = (float *)malloc(iterations*sizeof(float));                ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ temp.c:17:12: warning: incompatible pointer types assigning 'double *'       'float *' [-wincompatible-pointer-types]     lamdas = (float *)malloc(iterations*sizeof(float));            ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ temp.c:12:9: warning: unused variable 'trans_time' [-wunused-variable]     int trans_time = 1;         ^ temp.c:11:13: warning: unused variable 'cumulative_x' [-wunused-variable]     int i,j,cumulative_x,count = 0; 

the answer right there in front of you. idea compile warnings enabled, , improve code until warnings gone. 1 more example of why true.

final(?) edit

i found few more inconsistencies in code. here complete, working version.

a few things note:

  • to random number between 0 , 1 (not including 0) best thing is:

(rand()+1.0)/rand_max

using % operator give non-uniform distribution (greater chance of smaller numbers - try , see).

  • you test collision flag being set when collisions - means no collisions found / counted (you allow collisions packets have had collisions...)
  • i made changes in code according things mentioned above
  • i added printf statements
  • i fixed formatting / indentation

#include<stdio.h> #include<stdlib.h> #include<math.h> #include<time.h>  int main() {   int no_packets = 10000;   double lamda = 0.1,*lamdas;   int i,j,cumulative_x,count = 0;   int trans_time = 1;   double temp,*throughput;   int iterations = 1/0.1;   printf("iterations: %d\n",iterations);   throughput = malloc(iterations*sizeof(*throughput));   lamdas = malloc(iterations*sizeof(*lamdas));   int *pkt_collision;   double *pkt_holder;   int k=0;   float p;   for(k=0; k<iterations;k++)   {     lamdas[k]=0.1*(k+1);     printf("lamdas: %f\n",lamdas[k]);   }    int l=0;    while(lamda<1.05) // avoid rounding errors   {     count = 0;     temp = lamdas[l] * no_packets;     int avg_packets = (int)temp;     pkt_holder = (double *)malloc(avg_packets*sizeof(double));     pkt_collision = (int *)malloc(avg_packets*sizeof(int));     srand(0);      for(i=0;i<avg_packets;i++)     {       pkt_holder[i] = 0.0;       pkt_collision[i] = 0;     }      printf("lamda = %.1f; avg_packets = %d\n", lamda, avg_packets);     int totaltried = 0;     for(i=1;i<avg_packets;i++)     {       double x;             {         //x = (rand() % 10000) / 10000.0;         x = rand() / (double)rand_max;       } while(x == 0);        double time_temp = -(double)log(x)/lamda;       pkt_holder[i]=pkt_holder[i-1] + time_temp;     }      for(i=1;i<avg_packets;i++)     {       for(j=i;j<avg_packets;j++)       {         if((pkt_holder[j]-pkt_holder[i-1])<=5) // &&pkt_collision[j]==1)         {           pkt_collision[j] = 1;           pkt_collision[i-1] = 1;         }         else           break;       }     }      for(i=0;i<avg_packets;i++)     {       if(pkt_collision[i] == 0)       {         count++;       }     }     throughput[l] = (float) count/no_packets;     printf("count = %d; throughput[%d] = %f\n", count, l, throughput[l]);     printf("freeing now...\n");     free(pkt_holder);     free(pkt_collision);     lamda+=0.1;     l++;   }    printf("sucess: %d\n",count);   return 0; } 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -