在C语言中通过函数传递指针变量

在C语言中通过函数传递指针变量,c,arrays,pointers,argument-passing,C,Arrays,Pointers,Argument Passing,我在这里有一些奇怪的行为。任何帮助都会很好 我是这样开始的: 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

我在这里有一些奇怪的行为。任何帮助都会很好

我是这样开始的:

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



}

现在我得到一个类似这样的错误。有什么想法吗?

您是否在应用阈值中重新分配事件标志?如果是这样,您需要让调用者获取更新的指针

比如:

int apply_threshold (int *audio_samples, unsigned int size_of_audio, int **event_flags) {
    *event_flags = realloc ...
}

...

int number_of_flags = apply_threshold (lopass_samples, length, &event_flags);
编辑:回答更新的问题:

event_flags = (int*)realloc(event_flags, sizeof(int) * (flag+1)); // reallocate the size of the array
这将更改事件标志指针的本地副本。调用者将看不到更改。使用我上面描述的方法

编辑2:更详细的示例

void foo(int * v) {
    v = 0; // The local copy of main's myvar is now 0. main's actual myvar is unchanged
}

void bar(int ** v) {
    *v = 0; // Main's myvar is now 0, we have a pointer to it and can modify it.
}

int main() {
    int * myvar = (int *) malloc(1); // Allocate 1 byte and make myvar point at this byte.
    foo(myvar); // Call foo, passing a *copy of* myvar, which also points at the allocated byte
    bar(&myvar); // Call bar, passing a *pointer to* myvar, which again points to the allocated byte
}
编辑3:回答新问题


您的“长度”是整数还是字节数?您将其视为整数,如果它确实是字节数,则可能会导致错误。

您需要向指针传递指针或指针引用。realloc可能会将您的内存移动到另一个位置,而调用者不会注意到

  realloc() changes the size of the memory block pointed to by ptr to size bytes.  The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized.  If ptr is NULL, then  the
   call  is  equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr).  Unless ptr is NULL, it must have been returned by an earlier call to mal‐

loc()、calloc()或realloc()如果指向的区域已移动,则执行空闲(ptr)

是否可以包括打印意外值的代码和写入
事件\u标志的代码?没有这些,很难看出你做错了什么。目前,您没有初始化已分配的数据,因此它可能包含随机垃圾。这不应该发生。实际上是不是仅仅返回了打破它的apply_threshold?也就是说,在两个打印之间没有其他操作?然后,您可能意外地损坏了event_提取器堆栈帧中event_标志的值,导致缓冲区溢出。什么操作系统/编译器-你能运行valgrind吗?我们真的需要看看你是如何在每个函数中引用int指针的,否则你只是在传递一个指针。@Tim Martin&@Rup:apply_threshold(应用阈值)
event_flags++
在其中某个地方迭代这些值。我相信我已经发布了所有相关代码。如果需要进一步澄清,请告诉我。@Erik。我相信我在重新分配,但我可能做错了。@Eric Brotto:你在重新分配。请参见我的示例,
apply\u threshold
应该使用
int**event\u flags
,使用
*event\u flags
,并且应该使用
&event\u flags
@Erik调用该函数。这似乎奏效了。但是现在在我的循环中,在realloc之后,我得到:程序收到信号:“EXC_BAD_ACCESS”。此帧内部的上一帧(gdb无法通过此帧展开)。有什么想法吗?@Eric Brotto,我想你的问题是,
realloc
可能会“增加同一位置的内存”或“复制后在其他地方给你更大的内存块”,如果第二次发生,你的代码将无法工作,如果第一次发生,它会工作。@Eric Brotto:用你当前的代码更新,或者问一个新问题,你能不能更具体一点,我该怎么做来纠正这个问题?