开发者

Loop will not increment past 8?

开发者 https://www.devze.com 2023-02-15 20:16 出处:网络
I have a loop that involves a dynamically allocated array in C. For some reason it crashes after flag increments 7 times. This wasn\'t happening before I was reallocating the size of the array. Here i

I have a loop that involves a dynamically allocated array in C. For some reason it crashes after flag increments 7 times. This wasn't happening before I was reallocating the size of the array. Here is the code:

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;
    }   

Any ideas? Please keep in mind that the size of the array is indeed the same value as length. Her开发者_JAVA技巧e is an example of my errors:

Loop will not increment past 8?


EDIT 1

FILE ONE:

   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);

FILE TWO:

 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;
 }


First you shouldn't cast the return of realloc.

Then if I suppose that the type of that variable is int*

*event_flags[flag] = i;

There is one * too much no?

Edit: After your remark on leaving out the cast.

So if your event_flags is effectively int**, you are really on the wrong track. Seeing your use, I would guess you simply want an array of int instead. If you do that and then

event_flags[flag] = i;

without * everywhere, your problem should go away.

If you really need that indirection, you'd have to allocate not only the array event_flags but also all the individual arrays these pointers are pointing to, with something like

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


I think you may have a problem with the precedence of the * operator versus the [] operator. That is *event_flags[flag] and (*event_flags)[flag] do not reference the same memory location. The first one correspond to **(event_flags + flag) (probably not accessible), while the second one correspond to *((*event_flags) + flag) (what you want).

So, you should rewrite your code to:

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

精彩评论

暂无评论...
验证码 换一张
取 消