0

I have a code like the following, which works fine:

AVFrame *frame = NULL;
open_input(&frame);

where the input argument of open_input is something like: AVFrame **frame;

Now, I want to extend this code to work with an array of frames (say, N frames). I tried the following code but my code stops working after being compiled by gcc in MingW:

int i, N = 3;
AVFrame **frame;
frame = (AVFrame *) malloc(N * sizeof(AVFrame *));
for(i=0;i<N;i++){
   frame[i] = (AVFrame *) malloc(sizeof(AVFrame));
   open_input(&frame[i]);
}

Do you know, what is the problem?

11
  • First you don't need to cast malloc's return, then your first cast is false Commented Dec 23, 2013 at 15:10
  • @EoiFirst Did you also know that you don't have to not cast the malloc? Commented Dec 23, 2013 at 15:13
  • @self read this : stackoverflow.com/a/605858/2148420 Commented Dec 23, 2013 at 15:26
  • @EoiFirst read this: stackoverflow.com/a/14879184/2327831 Commented Dec 23, 2013 at 15:29
  • 2
    @self.: Don't go off in discussions like this: This question is tagged C, not C++. Next, you might aswell argue to write a script that copies all your .c files to a .cpp file... in C, casting malloc's return pointer is generally not done Commented Dec 23, 2013 at 15:33

3 Answers 3

2

If you want to allocate an array of frames, you could simply do

AVFrame *frame = malloc(N * sizeof(*frame));

accessing each element using frame[index]

A pointer to a pointer to AVFrame would only be required if you wanted an array of AVFrame arrays.

Sign up to request clarification or add additional context in comments.

Comments

0

You should probably be passing frame[i] to open_input rather than &frame[i]

I also think that this:

frame = (AVFrame *) malloc(N * sizeof(AVFrame *));

should be

frame = (AVFrame **) malloc(N * sizeof(AVFrame *));

I'd have thought your compiler might be warning about that too.

If you haven't already done so, then turn up the warning level on the compiler (probably /Wall on your compiler), and deal with the results.

1 Comment

You've given no description of the problem other than 'stops working', so it's not really possible to offer more help. Run under a debugger would be my next suggestion, as long as you've dealt with the compiler warnings (which you must be getting with /Wall, particularly if you've completely changed the type of the argument to open_input.)
0

&frame[i] is of type AVFrame **. It seems that open_input is expecting the argument of type AVFrame *.
Change

open_input(&frame[i]);  

to

open_input(frame[i]);   

Here frame[i] is of type AVFrame *.

And also do not cast the return value of malloc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.