array of pointers as argument in c functions

Kindermann

I'm stuck in C array pointer and passing it as argument into c functions. Here is the code where issues are involved:

char *src_filename1 = NULL;
AVFrame *frame = NULL;
AVFrame *frames_video1[3];
AVFrame *frames_video2[3];

int decode_packet(int *got_frame, int cached, AVFrame *frames_video[])
{
    ...
    frames_video[video_frame_count] = frame; //video_frame_count is 2 at the moment
    ...
}

int main()
{
    decode_video(src_filename1, frames_video1);
    ...
    printf("frames_video1[i]->pkt_size: %d\n", frames_video1[i]->pkt_size);
    ...
}
int decode_video(char *src_filename, AVFrame *frames_video[])
{
    ...
    decode_packet(&got_frame, 0, frames_video);
    ...
}

I could compile the code, but when I executed it, I encountered segmentation fault caused by frames_video1[i]->pkt_size How shall I pass arguments in this context? I find when dealing with char*[] and char** especially when using them as function arguments, my hands always get dirty...

EDIT NEW CODE: (which works!)

char *src_filename1 = NULL;
AVFrame *frame = NULL;
AVFrame frames_video1[3];
AVFrame frames_video2[3];

int decode_packet(int *got_frame, int cached, AVFrame frames_video[])
{
    ...
    AVFrame tmp;
    tmp = *frame;
    frames_video[video_frame_count] = tmp;
    ...
}

int main()
{
    decode_video(src_filename1, frames_video1);
    ...
    printf("frames_video1[i].pkt_size: %d\n", frames_video1[i].pkt_size);
    ...
}
int decode_video(char *src_filename, AVFrame frames_video[])
{
    ...
    decode_packet(&got_frame, 0, frames_video);
    ...
}

Let me briefly summarize the main issue of the old code: It had little to do with initialization, the main problem was the passing array of pointers as argument, was really about passing argument all the way properly: after hearing the suggestion from @SF., I changed the argument from AVFrame *frames_video[] to AVFrame frames_video[], and supprisingly everything worked well! It also means AVFrame frames_video1[] has been finally successfully "received" by decode_packet function... "Arrays behave as pointers in so many ways that you're getting drowned in pointer-to-pointer bog.", that's what he said. Actually, I still don't quite really understand how c pointers (especially when associated with arrays) work in a deeply sense...

John Bollinger

How shall I pass arguments in this context?

The code you presented is fine, as far as it goes. The arguments to the function calls agree with the functions' parameters (and surely you could induce your compiler to warn you of any cases where that was not so). Moreover, if you pass an array as a function argument then the called function will see the same element values as the caller, and if it sets different values for any of that array's elements then the effect will be visible to the caller.

I'm uncertain why you are confident that the segmentation fault arises from evaluating the expression frames_video1[i]->pkt_size, but perhaps you determined it via a debugger. In that case, there are only two likely explanations:

  1. variable i is either less than 0 or greater than 2, so that you are trying to access an out-of-bounds array element. Check your loop bounds, and be especially aware that the maximum index for an N-element array is N-1, not N.

  2. frames_video1[i] is not a valid pointer. It might never have been set to a valid pointer, or it might be a pointer to dynamic memory that since has been freed, or its value may have been corrupted. Certainly the initialization issues discussed in the other answers and in the comments on them point to one avenue by which this alternative could be exercised, but since you seem to discount that as a possible cause, I will not cover that ground again.

Overall, you present only skeleton code, not a working example by which the problem can actually be demonstrated. The answers you will receive are necessarily constrained by that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related