lundi 22 avril 2019

Best way to wrap a char* in C++?

In my code I use buffers currently allocated this way:

char* buf1 = (char*)malloc(size);

However at some points in the code I want to reassign the pointer to some place else in memory. The problem is that there are other places in the code that still need to be able to access the pointer buf1.

What's the best way to do this in C++? Right now I am considering writing a struct with a single char* in it, then allocating an object of this struct type and passing it to the places where I need to, and referring to wrapped pointer to get the current value of buf1.

However it seems that this is similar to what unique_ptr does. If I use unique_ptr how can I wrap a char* with it? I had some trouble with testing this and I'm not sure it's supported.

Aucun commentaire:

Enregistrer un commentaire