2011-03-10 66 views
0

我有一個循環涉及在C中動態分配的數組。出於某種原因,它在flag增量7次之後崩潰。在我重新分配數組的大小之前,這並沒有發生。這裏是代碼:循環不會增加過去8?

for (int i = 0; i < length-1; i++) 
{ 

    if (audio_samples[i] > threshold && run) 
    { 

     *event_flags = (int*)realloc(*event_flags, sizeof(int)*(flag+1)); // reallocate the size of the array 
     *event_flags[flag] = i; 
     // printf("FLAG CREATED! %i\n ", i); 
     printf("EVENT FLAG %i %i\n",flag, *event_flags[flag]); 
     if (flag >5) { 
      printf("%d\n", i); 
     } 

     flag++; 
     run = false; 
    } 

任何想法?請記住,數組的大小的確與長度相同。這裏是我的錯誤的一個例子: enter image description here


編輯1

文件中的一個:

int *event_positions = (int *) malloc(1 * sizeof(int)); // let us start with 1 and then add more within the method. This should continue until we have all the flags we want. 
    int number_of_flags = event_extractor(vocal_data, size, event_positions); 

文件中的兩個:

float g_THRESHOLD_FACTOR = 2.3; // THIS INCREASES THE THRESHOLD VALUE. 


int event_extractor (int *audio_samples, unsigned int size_of_audio ,int *event_flags) 
{ 

int length = (int)size_of_audio; 


// * * * * * * * * * * * * * * * * * * 
// RECTIFY VALUES (MAKE ABSOLUTE) (MAKE ALL POSITIVE) 
int *rectified_audio = (int *) malloc(length * sizeof(int)); // I took this line from wave header reader. The number is the number of samples of the hip hop track. 
make_values_absolute(audio_samples, length, rectified_audio); 


    // If I convert to signed ints here would the method run more efficiently? 

// * * * * * * * * * * * * * * * * * * * * 
// LOW PASS FILTER 
int *lopass_samples = (int *) malloc(length * sizeof(int)); // I took this line from wave header reader. The number is the number of samples of the hip hop track. 
lopass(rectified_audio, length,0.5, lopass_samples); 



int number_of_flags = apply_threshold (lopass_samples, length, &event_flags); 


printf("\n\n\n NUMBER OF EVENTS AAAA --- %d\n", number_of_flags); 

for (int i = 0; i < number_of_flags; i++) { 
    printf("FLAG %i -- %d \n", i, event_flags[i]); 
} 



return number_of_flags; 
} 


int apply_threshold (int *audio_samples, unsigned int size_of_audio, int **event_flags) 
    { 


int flag = 0; // this will be the number of flags that I have 
bool run = true; // this will make sure that a minimum amount of time passes before I grab another flag. It's a guard. 
int counter = 0; // this is the counter for the above guard. 





printf("\n\nCURRENT MINIMUM TIME: 20100 SAMPLES \n\n"); 

// event_flags[0] = 1; // this first one is a dud. within the loop we will automatically start adding flags 


int threshold = calculate_threshold_value(audio_samples, size_of_audio); 

printf("\n\n this is the threshold %d \n\n", threshold); 

int length = (int)size_of_audio; 

printf("LENGTH OF VOCAL AUDIO %d \n", length ); 


for (int i = 0; i < length-1; i++) 
{ 

    if (audio_samples[i] > threshold && run) 
    { 

     // ** is this realloc working ? 
     // event_flags = (int*)realloc(event_flags, sizeof(int) * (flag+1)); 
     *event_flags = (int*)realloc(*event_flags, sizeof(int)*(flag+1)); // reallocate the size of the array 
     *event_flags[flag] = i; 
     // printf("FLAG CREATED! %i\n ", i); 
     printf("EVENT FLAG %i %i\n",flag, *event_flags[flag]); 
     if (flag >5) { 
      printf("%d\n", i); 
     } 

     flag++; 
     run = false; 




    } 

    if (!run) { 
     counter++; 
     if (counter > 20100) { // hardcode minimum size for now. 
      counter = 0; 
      run=true; 
     } 
    } 

} 

printf("\n\n\n NUMBER OF EVENTS --- %d\n", flag); 

for (int i = 0; i < flag; i++) { 
    printf("FLAG %i -- %d\n", i, *event_flags[i]); 
} 



printf("\nFIVE samples before and after my second flag: \n 0 should indicate a reach in the threshold\n"); 

for (int i = 0; i <10 ; i++) { 
    printf("VOCAL SAMPLE %i %i \n", i-5,audio_samples[*event_flags[1]+i-5]); 
} 


return flag; 
} 
+0

你能顯示代碼的位置你在哪裏'malloc'ing數組? – phimuemue 2011-03-10 13:40:29

+0

你可以顯示'event_flags'的定義。我假設'int ** event_flags',但想檢查。 – 2011-03-10 13:45:12

+0

請看我的第一個編輯。 – 2011-03-10 13:50:39

回答

2

首先你不應該投下的回報。

那麼,如果我想,該變量的類型是int*

*event_flags[flag] = i; 

有一個*太多了不是嗎?

編輯:在您離開演員陣容之後的評論。

所以如果你的event_flags有效int**,你真的走錯了路。看到你的使用,我猜你只需要一個int而不是數組。如果你這樣做,然後

event_flags[flag] = i; 

沒有*無處不在,你的問題應該消失。

如果你真的很需要那間接,你就必須分配不僅陣列event_flags也是所有這些指針都指向單獨的陣列,具有類似

for (size_t j = startvalue; j < something; ++j) 
    event_flags[j] = malloc(whatever); 
+0

嗯..不完全確定你的意思?我應該完全擺脫演員陣容嗎? – 2011-03-10 13:47:23

+1

是的。在C中鑄造幾乎總是表示不正確的代碼。也就是說,如果你的代碼沒有編譯或者沒有強制轉換就給出警告,那麼你可能做錯了。如果它在沒有演員的情況下正常工作,請刪除演員陣容,以免它向人們宣讀您正在執行的醜陋黑客代碼。 – 2011-03-10 14:17:51

+0

我已經擺脫了演員陣容,但後來'從void *無效轉換爲int **'。任何幫助? – 2011-03-10 15:13:44

1

我想你可能有問題與運營商的*運營商的優先級相比。即*event_flags[flag](*event_flags)[flag]不引用相同的內存位置。第一個對應於**(event_flags + flag)(可能不可訪問),而第二個對應於*((*event_flags) + flag)(你想要的)。

所以,你應該重寫你的代碼:

int** event_flags; 
// ... 
*event_flags = realloc(*event_flags, sizeof(int) * (flag + 1)); 
(*event_flags)[flag] = i;