2014-01-31 24 views
1
#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; 
       do 
       { 
       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 this //statement 
     l++; 
} 


printf("Sucess: %d\n",count); 
return 0; 
} 

在這個程序中,我試圖模擬純阿羅哈的吞吐量。glibc錯誤檢測不知道什麼是錯

lamda 0.1開始到lamda = 1,我試圖找到與這些值對應的吞吐量。

在while循環中,我試圖通過在每次迭代循環結束時釋放它們的內存來重新分配每次迭代中pkt_holder和pkt_collision的大小。

當我試圖在eclipse中調試時,它顯示glibc錯誤時執行空閒(pkt_collision)。

任何幫助或建議將不勝感激。

謝謝

+0

'lambda'和'thoughput'應該是float *你有double *。 'lambda [k]'是否會導致訪問無效內存的問題 –

回答

3

我懷疑你是覆蓋在下面的序列東西:

您分配iterations插槽lamdas,但是當你迭代,你不與一個整數值迭代,但是有一個浮點索引。因此,您可能會得到比您想象的更多的迭代(這與無法完全代表浮點數0.1)有關。

解決的辦法是寫

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

由於指針*pkt_collisionlamdas後立即保存,因爲大多數編譯器店指針前右所需的一些信息free(這是不是標準,但是這是我的經驗),寫入lamdas分配空間的末尾,實際上寫入free使用的空間。

我認爲這將解決它。

編輯只注意到您聲明lamdasdouble*,但鑄造的指針指向它作爲一個float*。你沒有看到編譯器警告嗎?

始終啓用編譯器警告和注意可能

所以,你有兩個問題。您需要分配足夠的內存:

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

並使用整數作爲循環變量。編譯器就知道了,得到它的權利 -

順便說一句,你可以通過編寫

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

這樣,您就不必記住什麼類型是防止將來出現此問題。

UPDATEgcc -Wall編譯代碼給出:

temp.c:16:16: warning: incompatible pointer types assigning to 'double *' from 
     'float *' [-Wincompatible-pointer-types] 
    throughput = (float *)malloc(iterations*sizeof(float)); 
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
temp.c:17:12: warning: incompatible pointer types assigning to 'double *' from 
     '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; 

答案是正確的,在你的面前。在啓用所有警告的情況下進行編譯是一個非常好的主意,並且在所有警告消失之前改進您的代碼。這僅僅是爲什麼這是事實的一個例子。

FINAL(?)編輯

我發現代碼中的幾個矛盾。這是完整的工作版本。

有幾件事情需要注意:

  • 得到0和1(不包括0)之間的隨機數可能是做的最好的事情是:

(rand()+1.0)/RAND_MAX

使用%運營商將給你一個不均勻的分佈(更小的數字的機會更大 - 嘗試它,你會看到)。

  • 你測試了collision標誌,當你看的碰撞被設置 - 這意味着沒有衝突,會發現/數(只允許碰撞與有衝突的包......)
  • 我做了一些根據我上面
  • 提到的東西,在代碼的變化我加了一些printf語句
  • 我固定某些格式/縮進

#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) // to 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; 
     do 
     { 
     //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; 
} 
4

這就是爲什麼你不應該做的事:

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

這種錯配和分配一半的預期空間。它應該是:

lambdas = malloc(iterations * sizeof *lambdas); 

不要手動重複類型,它是有風險和冗長的。將其「鎖定」到目標指針並使用sizeof

另外,don't cast the return value of malloc() in C

+0

類型不匹配的好處。我認爲使用浮點循環計數器也可能是問題的根源。 – Floris

+1

只需稍作修改:使用'sizeof(* lambdas)'而不是'sizeof * lambdas'。 –

+0

注意同樣的問題出現在吞吐量上,但錯誤很可能是由這個造成的。由於數組已分配並填充但從未使用,因此具有諷刺意味。 – Floris

相關問題