Home > Back-end >  Consult, how to set up a pointer to an unsigned char * smart Pointers?
Consult, how to set up a pointer to an unsigned char * smart Pointers?

Time:09-30

When I was in the treatment of the audio, because access to content and use it not in the same place, be afraid of a memory leak occurs, so I want to by smart Pointers to manage a uint8_t * pointer, excuse me how to use this from right, thank you,


The original program is like this:

Int len=10;

Buff uint8_t *=new uint8_t (len);

Memcpy (buff, SRC, len);

Printf (" % x ", buff [i++]);


My attempt is like this:

Int len=10;

From & lt; Uint8_t * & gt; Buff (new uint8_t [10]);

Memcpy (buff, SRC, len);//visit an error, can't will buff to a void *

Printf (" % x ", buff [i++]);//visit an error, there is no operator to match the "[]"



Make_share also cannot use

Can you tell me how to use only Pointers here?

CodePudding user response:

Doesn't seem to support the operator from [], seems to be a c + + support after 17
To switch to unique_ptr.
//from & lt; Uint8_t & gt; Buff (new uint8_t [10], default_delete & lt; Uint8_t [] & gt; ());
Unique_ptr & lt; Uint8_t [] & gt; Buff (new uint8_t [10]);
Memcpy (buff. The get (), SRC, len);
Printf (" % x ", buff [i++]);