Eric Brotto
Eric Brotto

Reputation: 54251

Accessing data in an array with pointers

I'm new to pointers and so am having a bit of difficulty. I do believe though that the solution to this should be easy for you experts. The problem is in the code.

 // flags is an array of data that I create, manipulate, but now having trouble accessing elsewhere. 
int *flags = (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, flags);

// I want to use flags here, but it doesn't work 

place_effects_on_events(vocal_data, flags, number_of_flags , events_with_effects);

THE OTHER METHODS:

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

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

    // the data prints absolutely correctly here.   
for (int i = 0; i < number_of_flags; i++) {
    printf("FLAG %i  -- %d \n", i, flags[i]);
}
}





 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. 




int threshold = calculate_threshold_value(audio_samples, size_of_audio);



int length = (int)size_of_audio;




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

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


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

        flag++;
        run = false;


    }   

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



}


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

return flag;

}

THE PROBLEM IS HERE

 void place_effects_on_events (int *vocal_samples, int *flags, int number_of_flags ,int *events_with_effects)
 {


// here the data does not print correctly 
for (int i = 0; i < number_of_flags; i++) {
    printf("FLAG %i  -- %d\n", i,  flags[i]);
}

Upvotes: 1

Views: 353

Answers (2)

Srikanth
Srikanth

Reputation: 11994

you are passing flags to event_extractor. Try passing &flags to it. I think you want flags to be updated after calling event_extractor function. One more point: you are not returning number_of_flags from event_extractor function.

Upvotes: 0

Alexander Gessler
Alexander Gessler

Reputation: 46607

You should pass a pointer to flags to event_extractor:

int number_of_flags = event_extractor(vocal_data, size, &flags);

and change its prototype to

int event_extractor (int *audio_samples, unsigned int size_of_audio, int **flags)

Otherwise, flags itself is never updated and still points to the first malloced memory area. Since this memory area no longer exists at this time, accessing its contents will cause undefined results.

Upvotes: 2

Related Questions