【发布时间】:2011-07-12 16:30:45
【问题描述】:
我在这里遇到了一些奇怪的行为。任何帮助都会很棒。
我是这样开始的:
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);
// HERE I WOULD LIKE TO USE THE VALUES OF event_positions BUT THE ARE WEIRD I.E. THEY DON'T MATCH THE VALUES BEING PRINTED IN THE LAST METHOD.
事件提取器然后将变量传递给另一个方法。简化后如下所示:
int event_extractor (int *audio_samples, unsigned int size_of_audio ,int *event_flags)
{
int number_of_flags = apply_threshold (lopass_samples, length, event_flags);
// PRINT ARRAY event_flags HERE
// VALUES ARE INCORRECT AND WEIRD
}
最后一种方法:
int apply_threshold (int *audio_samples, unsigned int size_of_audio, int *event_flags)
{
// DO SOME STUFF HERE.
// PRINT THE ARRAY WHICH SHOW THE CORRECT VALUES.
}
我希望这很清楚。基本上我有一个数组,我将其作为参数传递,并且在方法完成后无法访问这些值。
编辑 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);
第二个文件:
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;
for (int i = 0; i < length; i++)
{
if (audio_samples[i] > threshold && run)
{
// ** is this realloc working ?
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] );
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;
}
编辑 2
我已经根据 Erik 的模型更新了我的代码。我的循环现在看起来像这样
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] );
flag++;
run = false;
}
现在我收到一个看起来像这样的错误。有什么想法吗?
【问题讨论】:
-
能否包含打印意外值的代码和写入
event_flags数组的代码?没有它,很难看出你做错了什么。就目前而言,您不会初始化您分配的数据,因此它可能包含随机垃圾。 -
这不应该发生。它实际上只是 apply_threshold 的返回破坏了它 - 即两个打印之间没有其他操作?然后,您可能通过溢出其他地方的缓冲区而意外损坏了 event_extractor 堆栈帧中 event_flags 的值。什么操作系统/编译器 - 你可以运行 valgrind 吗?
-
我们真的需要看看你是如何在每个函数中引用 int 指针的,否则你只是在传递一个指针。
-
-
我相信我已经发布了所有相关的代码。如果需要更多说明,请告诉我。
标签: c arrays pointers argument-passing