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]);
}
You should pass a pointer to
flagstoevent_extractor:and change its prototype to
Otherwise,
flagsitself is never updated and still points to the firstmalloced memory area. Since this memory area no longer exists at this time, accessing its contents will cause undefined results.