C++ CString 到 char*
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/559483/
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
CString to char*
提问by Mark Lakewood
We are using the CString class throughout most of our code. However sometimes we need to convert to a char *. at the moment we have been doing this using variable.GetBuffer(0) and this seems to work ( this mainly happens when passing the Csting into a function where the function requires a char *). The function accepts this and we keep going.
我们在大部分代码中都使用 CString 类。但是有时我们需要转换为 char *。目前我们一直在使用 variable.GetBuffer(0) 来做这件事,这似乎有效(这主要发生在将 Csting 传递到函数需要 char * 的函数时)。该函数接受这一点,我们继续前进。
However we have lately become worried about how this works, and whether there is a better way to do it.
然而,我们最近开始担心这是如何工作的,以及是否有更好的方法来做到这一点。
The way i understand it to work is it passes a char pointer into the function that points at the first character in the CString and all works well.
我理解它的工作方式是它将一个字符指针传递给指向 CString 中第一个字符的函数,并且一切正常。
I Guess we are just worried about memory leaks or any unforseen circumstances where this might not be a good idea.
我猜我们只是担心内存泄漏或任何不可预见的情况,这可能不是一个好主意。
回答by Mark Ransom
If your functions only require reading the string and not modifying it, change them to accept const char *
instead of char *
. The CString
will automatically convert for you, this is how most of the MFC functions work and it's really handy. (Actually MFC uses LPCTSTR
, which is a synonym for const TCHAR *
- works for both MBC and Unicode builds).
如果您的函数只需要读取字符串而不修改它,请将它们更改为 acceptconst char *
而不是char *
。该CString
会自动转换为你,这是多么大多数MFC职能的工作,这真的很方便。(实际上 MFC 使用LPCTSTR
,它是const TCHAR *
- 适用于 MBC 和 Unicode 版本的同义词)。
If you need to modify the string, GetBuffer(0)
is very dangerous - it won't necessarily allocate enough memory for the resulting string, and you could get some buffer overrun errors.
如果您需要修改字符串,这GetBuffer(0)
是非常危险的 - 它不一定会为结果字符串分配足够的内存,并且您可能会遇到一些缓冲区溢出错误。
As has been mentioned by others, you need to use ReleaseBuffer
after GetBuffer
. You don't need to do that for the conversion to const char *
.
正如其他人所提到的,您需要使用ReleaseBuffer
after GetBuffer
。您无需为转换为const char *
.
回答by Rajesh R Subramanian
@ the OP: >>> I Guess we are just worried about memory leaks or any ...
@ OP: >>> 我猜我们只是担心内存泄漏或任何...
Hi, calling the GetBuffer method won't lead to any memory leaks. Because the destructor is going to deallocate the buffer anyway. However, others have already warned you about the potential issues with calling this method.
您好,调用 GetBuffer 方法不会导致任何内存泄漏。因为析构函数无论如何都会释放缓冲区。但是,其他人已经警告过您调用此方法的潜在问题。
@Can >>> when you call the getbuffer function it allocates memory for you.
@Can >>>当您调用 getbuffer 函数时,它会为您分配内存。
This statement is not completely true. GetBuffer(0) does NOT allocate any memory. It merely returns a pointer to the internal string buffer that can be used to manipulate the string directly from "outside" the CString class.
这种说法并不完全正确。GetBuffer(0) 不分配任何内存。它仅返回一个指向内部字符串缓冲区的指针,该缓冲区可用于直接从 CString 类的“外部”操作字符串。
However, if you pass a number, say N to it like GetBuffer(N), and if N is larger than the current length of the buffer, then the function ensures that the returned buffer is at least as large as N by allocating more memory.
但是,如果你传递一个数字,像 GetBuffer(N) 一样给它说 N,并且如果 N 大于缓冲区的当前长度,那么该函数通过分配更多内存来确保返回的缓冲区至少与 N 一样大.
Cheers, Rajesh. MVP, Visual ++.
干杯,拉杰什。MVP,视觉++。
回答by Codingday
when you call the getbuffer function it allocates memory for you. when you have done with it, you need to call releasebuffer to deallocate it
当您调用 getbuffer 函数时,它会为您分配内存。完成后,您需要调用 releasebuffer 来释放它
回答by Gregor Brandt
try the documentation at http://msdn.microsoft.com/en-us/library/awkwbzyc.aspxfor help on that.
尝试http://msdn.microsoft.com/en-us/library/awkwbzyc.aspx上的文档以获得这方面的帮助。