I have a pointer str:
str
char* str = new char[10];
I use a memory block strto store data.
How can I allocate more bytes for the buffer pointed strto and not lose the old data stored in the buffer?
Use std :: string instead . It will do what you need without worrying about distribution, copying, etc. You can access raw memory with the c_str () function.
It will even work well for you. std::vector<char>
std::vector<char>
new[] , ( memcpy()), delete[] , , .
new[]
memcpy()
delete[]
new. malloc, realloc free ( malloc/realloc/free new/delete).
new
malloc
realloc
free
++, std::vector. , , std::string ( std::vector, ). 10 . , , , , 10 , , , . , , . , , , , , , .
int main() { std::string s; s.reserve( 10 ); // do whatever with s }
, std::string std::Vector , .
realloc - , . malloc/free new/delete,
str .
realloc: http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/ , ++ ( , , , std::vector<char>).
. , , .
: C , realloc, . , , . , , .
, .
, ? , , , , , . , , .
, STL. -, "". std::string, , .
Source: https://habr.com/ru/post/1736618/More articles:Error saving iPhone Coredata - iphoneCmake suddenly cannot find my source files anymore - cmakehow to check a document in a document library programmatically in sharepoint - documentOn MacOSX, in a C ++ program, what guarantees can I get in an IO file - c ++WPF: stopping ContextMenu from closing - c #How to set a model attribute for each action - grailswhy can't I comment on lines in visual mode - vimОкно верхнего уровня в X Window System - c++How to work with Hibernate hql result with multiple connections in Object-Oriented Way? - javaHow to properly use multiple restrictions in C # Generics? - genericsAll Articles