简体   繁体   English

如何在C ++中为缓冲区分配更多内存?

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

I have pointer str : 我有指针str

char* str = new char[10];

I use the memory block str points to to store data. 我使用内存块str点来存储数据。

How can I allocate more bytes for the buffer pointed to by str and not lose old data stored in the buffer? 如何为str指向的缓冲区分配更多字节而不丢失缓冲区中存储的旧数据?

Use std::string instead . 请改用std :: string 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. 它可以满足您的需要而无需担心分配,复制等。您仍然可以通过c_str()函数访问原始内存。

Even std::vector<char> will work well for you. 即使是std::vector<char>也能很好地适合你。

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

You cannot using the new construction. 你不能使用new结构。 For that you need to use the good old malloc , realloc , and free (do not mix malloc/realloc/free and new/delete). 为此你需要使用好的旧mallocreallocfree (不要混合malloc / realloc / free和new / delete)。

If you are really using C++, the most correct solution would be to use std::vector. 如果你真的使用C ++,最正确的解决方案是使用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). 我假设您没有将该信息用作标准字符串,在这种情况下,您应该使用std :: string(这是std :: vector的特化,所以没什么大不了的)。 You are creating at least 10 chars. 你正在创造至少10个字符。 This gives me the hint that you are probably quite sure that you'll need 10 chars, but maybe you'll nedd more. 这给了我一个提示,你可能很确定你需要10个字符,但也许你会更多。 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. 正如其他人已经指出的那样,使用std :: string或std :: Vector会让你忘记复制,调整大小或删除保留的内存。

The realloc function is what you are searching for. realloc函数就是您要搜索的内容。 You had to use malloc/free instead of new/delete to use it 你必须使用malloc / free而不是new / delete来使用它

您必须分配一个不同的,更大的字符串数组,并将数据从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. 答案是,在C中存在一种叫做realloc的东西,允许你做以下事情。 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. 这正是STL的字符串和向量正在为你做的事情。 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. 使用std :: string可能是解决问题的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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