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?

Blue Sky
  • 293
  • 1
  • 5
  • 14
  • First you don't need to cast malloc's return, then your first cast is false – Yabada Dec 23 '13 at 15:10
  • @EoiFirst Did you also know that you don't have to not cast the malloc? – this Dec 23 '13 at 15:13
  • @self read this : http://stackoverflow.com/a/605858/2148420 – Yabada Dec 23 '13 at 15:26
  • @EoiFirst read this: http://stackoverflow.com/a/14879184/2327831 – this Dec 23 '13 at 15:29
  • @self Lets stop here, I just desagree :) – Yabada Dec 23 '13 at 15:31
  • @EoiFirst No problem, I though we were sharing information. – this Dec 23 '13 at 15:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43771/discussion-between-eoifirst-and-self) – Yabada Dec 23 '13 at 15:31
  • 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 – Elias Van Ootegem Dec 23 '13 at 15:33
  • @self.: Can't help myself, but none of the arguments in your linked answer make sense if you use `malloc` as you should use it: `someVar = malloc(sizeof(*someVar));` change the type of `someVar`, and you don't have to change your `malloc` call. You'd have to refactor all related code, changing your cast. casting actually makes for more error-prone development, IMO – Elias Van Ootegem Dec 23 '13 at 15:40
  • @EliasVanOotegem Those aren't my arguments, I was merely sharing information with a curious member of SO. *Don't go off in discussions like this*; it's a good thing you don't own the site, it almost looks like you are trying to exert influence. Discussion ended. – this Dec 23 '13 at 15:42
  • @self.: I'm not trying to exert influence, merely pointing out [that you're not using comments the way they were intended by the people who _do_ own the site](http://stackoverflow.com/help/privileges/comment). If I wanted to exert influence, I'd have flagged your comments for moderation attention. I'm not petty, so I didn't, and besides: the mods have enough work already – Elias Van Ootegem Dec 23 '13 at 17:30

3 Answers3

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.

simonc
  • 41,632
  • 12
  • 85
  • 103
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.

Will Dean
  • 39,055
  • 11
  • 90
  • 118
  • 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.) – Will Dean Dec 23 '13 at 15:15
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.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264