將char *分配給字符串而不復制[duplicate]

[英]Assign char* to string without copying [duplicate]


This question already has an answer here:

這個問題在這里已有答案:

this is quite a simple question, however I'm finding it tricky. I want to treat a char* as if it were a std::string, for instance:

這是一個非常簡單的問題,但我發現它很棘手。我想將char *視為std :: string,例如:

    char *p = ...; // read a huge chuck from a file

    std::string s(p); // this is not what I want

So, if I use the constructor, I get a copy of p, which is a waste of memory and time. Is it possible somehow to avoid this, and "assign" the std::string content to a pre-existing address?

所以,如果我使用構造函數,我會得到一個p的副本,這會浪費內存和時間。有可能以某種方式避免這種情況,並將std :: string內容“分配”到預先存在的地址嗎?

Any other idea is more than welcome!

任何其他想法都非常受歡迎!

Thanks!

5 个解决方案

#1


11  

Is it possible somehow to avoid this, and "assign" the std::string content to a pre-existing address?

有可能以某種方式避免這種情況,並將std :: string內容“分配”到預先存在的地址嗎?

No.

#2


2  

std::string does not and cannot possibly support this, as it owns the string buffer.

std :: string沒有也不可能支持這個,因為它擁有字符串緩沖區。

Which means that it will eventually have to free the memory, or reallocate it in case you change the string to have a different length. If not earlier, then it must do so when the program exits.

這意味着它最終必須釋放內存,或者在將字符串更改為具有不同長度的情況下重新分配它。如果不早,那么當程序退出時必須這樣做。

Now, what is string supposed to do with some unknown block of memory that it got via a pointer? Is this memory allocated on the heap or on the stack, or maybe readonly memory from the data segment? There is nothing string could possibly do that is valid and won't either leak or cause a crash in one or the other situation.

現在,什么是字符串應該通過指針獲得一些未知的內存塊?這個內存是在堆上還是在堆棧上分配的,或者是數據段中的只讀內存?沒有任何字符串可能做的有效,並且不會泄漏或導致在一種或另一種情況下崩潰。

#3


1  

No, because the std::string generally expects more than a char* can provide, most notably reallocation of the storage into totally different place of memory. Also a std::string isn't guaranteed to be null-terminated, it just provides a begin() and an end().

不,因為std :: string通常需要的不僅僅是char *可以提供,最值得注意的是將存儲重新分配到完全不同的內存位置。此外,std :: string不保證以null結尾,它只提供begin()和end()。

But note that std::string and char[] have quite common interfaces:

但請注意,std :: string和char []有相當常見的接口:

  • you can index them with numbers and obtain chars,
  • 你可以用數字索引它們並獲得字符,

  • you can call std::begin and std::end of them and get random access iterators, so that algorithms like sort can operate on them freely.
  • 你可以調用它們的std :: begin和std :: end並獲得隨機訪問迭代器,這樣像sort這樣的算法可以自由地對它們進行操作。

That's the core of C++ standard template library- containers and algorithms are separate, and the same algorithm can operate on a std::string and a char[].

這是C ++標准模板庫的核心 - 容器和算法是分開的,相同的算法可以在std :: string和char []上運行。

Of course char* isn't char[], but OTOH a pair of char*s looks exactly like begin(char[]) and end(char[]), so that enough allows you to connect it to STL utilities that work in term of random access iterators.

當然char *不是char [],但是OTOH一對char *看起來就像begin(char [])和end(char []),所以足夠允許你將它連接到工作的STL實用程序隨機訪問迭代器的術語。

#4


1  

No the wide-spreading std::string implementations do not implement a such feature. Even using the placement new() because the internal members can change from one implementation to another, or from one version to another, or depending on some #define... There is also the option to provide your own std::string allocator, but this does not seem to be the way to deal with this kind of issue...

沒有廣泛傳播的std :: string實現沒有實現這樣的功能。甚至使用placement new()因為內部成員可以從一個實現更改為另一個實現,或者從一個版本更改為另一個版本,或者取決於某些#define ...還有提供自己的std :: string分配器的選項,但這似乎不是處理這類問題的方法......

Yes some string implementations allow buffer reuse as the RFA_string from Reuters Foundation API.

是的,一些字符串實現允許緩沖區重用作為Reuters Foundation API的RFA_string。

This idea has already been treated on some other questions/answers:

這個想法已經在其他一些問題/答案上得到了處理:

Moreover, there is also the rope data structure as for SGI STL used by boost...

此外,還有一個繩索數據結構,就像升級使用的SGI STL一樣......

#5


-2  

Why not use std::vector<char> ? For example:

為什么不使用std :: vector ?例如:

std::vector<char> data;
data.resize( size );  // resize this to how much you need 

char* p = &data[0];

// now you have a pointer to the internal data in std::vector 

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/10/11/72f9b9063526d6d89ef515d099088d57.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com