简体   繁体   中英

How to allocate more memory for a buffer in C++?

I have pointer str :

char* str = new char[10];

I use the memory block str points to to store data.

How can I allocate more bytes for the buffer pointed to by str and not lose old data stored in the buffer?

Use std::string instead . It will do what you need without you worrying about allocation, copy etc. You can still access the raw memory via the c_str() function.

Even std::vector<char> will work well for you.

new[]另一个缓冲区,在那里复制数据(使用memcpy() ),然后delete[]旧的缓冲区地址,将新的缓冲区地址分配给最初持有旧缓冲区地址的指针。

You cannot using the new construction. For that you need to use the good old malloc , realloc , and free (do not mix malloc/realloc/free and new/delete).

If you are really using C++, the most correct solution would be to use std::vector. I assume that you are not using that information as a standard string, in that case you should use std::string (which is an specialization of std::vector, so no big deal). You are creating at least 10 chars. This gives me the hint that you are probably quite sure that you'll need 10 chars, but maybe you'll nedd more. Maybe you are worried about the performance problems involved in allocating and deallocating memory. In that case, you can create your string and then reserve the estimated capacity that you expect you'll need, so there won't be any reallocation at least until you get to that limit.

int main()
{
    std::string s;
    s.reserve( 10 );
    // do whatever with s
}

As others have already pointed out, the use of std::string or std::Vector will get you the benefit of forgetting about copy, resizing or deleting the reserved memory.

The realloc function is what you are searching for. You had to use malloc/free instead of new/delete to use it

您必须分配一个不同的,更大的字符串数组,并将数据从str复制到新的字符串数组。

您可以使用realloc: http ://www.cplusplus.com/reference/clibrary/cstdlib/realloc/我想补充一点,这种方法不是受欢迎的c ++方法(根据您的需要,您可以使用std::vector<char>例如)。

Allocation is a bit like finding a parking place. You're asking here if it's possible to add a trailer on your car that has been parked for a fews days.

The answer is, in C there exists something called realloc that allows you to do following thing. If I have already enough place to add my trailer, do so. If not park in another place big enough for your trailer and your car, which is equivalent to copying your data.

In other words you'll get strong and random performance hits.

So what would you do in the real world? If you knew you might need to add some trailers to your car you'd probably park in a bigger place than required. And when exceeding the size required for the place, you'd move your car and your trailers to a place with a nice margin for future trailers.

That's precisely what the STL's string and vector is doing for you. You can even give them a hint of the size of your futures trailer by calling "reserve". Using std::string is probably the best answer to your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM