C++ 如何将char *复制到字符串中,反之亦然
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2564052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to copy char * into a string and vice-versa
提问by user295030
If i pass a char * into a function. I want to then take that char * convert it to a std::string and once I get my result convert it back to char * from a std::string to show the result.
如果我将 char * 传递给函数。然后我想将该 char * 转换为 std::string ,一旦我得到结果,将其从 std::string 转换回 char * 以显示结果。
- I don't know how to do this for conversion ( I am not talking const char * but just char *)
- I am not sure how to manipulate the value of the pointer I send in.
- 我不知道如何进行转换(我不是在说 const char * 而只是 char *)
- 我不确定如何操作我发送的指针的值。
so steps i need to do
所以我需要做的步骤
- take in a char *
- convert it into a string.
- take the result of that string and put it back in the form of a char *
- return the result such that the value should be available outside the function and not get destroyed.
- 接受一个字符 *
- 将其转换为字符串。
- 获取该字符串的结果并将其以 char * 的形式放回
- 返回结果,以便该值应该在函数之外可用并且不会被破坏。
If possible can i see how it could be done via reference vs a pointer (whose address I pass in by value however I can still modify the value that pointer is pointing to. so even though the copy of the pointer address in the function gets destroyed i still see the changed value outside.
如果可能的话,我可以看到它是如何通过引用与指针完成的(我按值传入的地址但是我仍然可以修改指针指向的值。所以即使函数中指针地址的副本被破坏我仍然看到外面的变化值。
thanks!
谢谢!
回答by James McNellis
Converting a char*
to a std::string
:
将 a 转换char*
为 a std::string
:
char* c = "Hello, world";
std::string s(c);
Converting a std::string
to a char*
:
将 a 转换std::string
为 a char*
:
std::string s = "Hello, world";
char* c = new char[s.length() + 1];
strcpy(c, s.c_str());
// and then later on, when you are done with the `char*`:
delete[] c;
I prefer to use a std::vector<char>
instead of an actual char*
; then you don't have to manage your own memory:
我更喜欢使用 astd::vector<char>
而不是实际的char*
; 那么你就不必管理自己的内存:
std::string s = "Hello, world";
std::vector<char> v(s.begin(), s.end());
v.push_back('const char* fn(const char*psz) {
std::string s(psz);
// do something with s
return s.c_str(); //BAD
}
'); // Make sure we are null-terminated
char* c = &v[0];
回答by hamishmcn
You need to watch how you handle the memory from the pointer you return, for example the code below will not work because the memory allocated in the std::string will be released when fn() exits.
你需要观察你如何从你返回的指针处理内存,例如下面的代码将不起作用,因为当 fn() 退出时,std::string 中分配的内存将被释放。
const char* fn(const char*psz) {
std::string s(psz);
// do something with s
char *ret = new char[s.size()]; //memory allocated
strcpy(ret, s.c_str());
return ret;
}
....
const char* p = fn("some text");
//do something with p
delete[] p;// release the array of chars
One solution is to allocate the memory in the function and make sure the caller of the function releases it:
一种解决方案是在函数中分配内存并确保函数的调用者释放它:
void fn(const char*in size_t bufsize, char* out) {
std::string s(psz);
// do something with s
strcpy_s(out, bufsize, s.c_str()); //strcpy_s is a microsoft specific safe str copy
}
....
const int BUFSIZE = 100;
char str[BUFSIZE];
fn("some text", BUFSIZE, str);
//ok to use str (memory gets deleted when it goes out of scope)
Alternatively, if you know an upper bound on the size of the string you can create it on the stack yourself and pass in a pointer, e.g.
或者,如果您知道字符串大小的上限,您可以自己在堆栈上创建它并传入一个指针,例如
char* lib::func(char*pStr)
{
std::string str(pStr);
char *outStr = new char[str.size()+1];
strcpy(outStr, str.c_str());
g_gc.push_back(outStr); // collect garbage
return outStr;
}
回答by Pankaj
You can maintain a garbage collector for your library implemented as
std::vector<char*> g_gc;
which is accessible in your library 'lib'. Later, you can release all pointers in g_gc at your convenience by calling lib::release_garbage();
你可以为你的库维护一个垃圾收集器,
std::vector<char*> g_gc;
它可以在你的库“lib”中访问。稍后,您可以在方便时通过调用释放 g_gc 中的所有指针lib::release_garbage();
void lib::release_garbage()
{
for(int i=0;i<g_gc.size();i++)
{
delete g_gc[i];
}
g_gc.clear();
}
release_garbage function will look like:
release_garbage 函数将如下所示:
##代码##In a single threaded model, you can keep this g_gc static. Multi-threaded model would involve locking/unlocking it.
在单线程模型中,您可以保持这个 g_gc 静态。多线程模型将涉及锁定/解锁它。