开发者

A new buffer created every time the program is called

I need to create a new buffer every time. Something like this

int bufcalc;

bufcalc++;

BufferSptr buf[bufcalc] (new Buffer (pMediaData->Size()) );
memcpy(buf[bufcalc]->GetData() ,pMediaData->GetData() , pMediaData->Si开发者_如何学JAVAze());

where bufcalc is type int and increments everytime.

   if(bufcalc>=2)
{
    DecodeBufferData ( buf[bufcalc-1], decodeInfo );
}

i get the following error:

variable-sized object 'buf' may not be initialized


BufferSptr buf[bufcalc] (new Buffer (pMediaData->Size()));

This is incorrect:

  • array sizes are bound to compile-time constants in C++.
  • multiple allocation is done with operator new[] and uses a constructor that can be called without arguments.


Try using std::vector instead of array. The std::vector expands as necessary and can use index variables like array:

std::vector<Buffer *> buf;

buf.push_back(new Buffer(pMediaData->Size()));

memcpy(buf[bufcalc]->GetData() ,pMediaData->GetData() , pMediaData->Size());

The std::vector is much safer than an array.
See also boost::shared_array and boost::shared_ptr.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜