开发者

A new buffer created every time the program is called

开发者 https://www.devze.com 2023-02-17 10:25 出处:网络
I need to create a new buffer every time. Something like this int bufcalc; bufcalc++; BufferSptr buf[bufcalc] (new Buffer (pMediaData->Size()) );

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

精彩评论

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