CString 转 char*
我们在大部分代码中都使用 CString 类。 然而有时我们需要转换为 char *。 目前我们一直在使用variable.GetBuffer(0) 执行此操作,这似乎有效(这主要发生在将 Csting 传递到需要 char * 的函数时)。 该函数接受了这一点,我们继续前进。
然而,我们最近开始担心它是如何工作的,以及是否有更好的方法来做到这一点。
我理解它的工作方式是将一个 char 指针传递到指向 CString 中第一个字符的函数中,并且一切正常。
我想我们只是担心内存泄漏或任何不可预见的情况,这可能不是一个好主意。
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.
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.
I Guess we are just worried about memory leaks or any unforseen circumstances where this might not be a good idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的函数只需要读取字符串而不修改它,请将它们更改为接受
const char *
而不是char *
。CString
会自动为您转换,这就是大多数 MFC 函数的工作方式,而且非常方便。 (实际上,MFC 使用LPCTSTR
,它是const TCHAR *
的同义词 - 适用于 MBC 和 Unicode 版本)。如果您需要修改字符串,
GetBuffer(0)
非常危险 - 它不一定会为结果字符串分配足够的内存,并且您可能会遇到一些缓冲区溢出错误。正如其他人提到的,您需要在
GetBuffer
之后使用ReleaseBuffer
。 您不需要为转换为const char *
执行此操作。If your functions only require reading the string and not modifying it, change them to accept
const char *
instead ofchar *
. TheCString
will automatically convert for you, this is how most of the MFC functions work and it's really handy. (Actually MFC usesLPCTSTR
, which is a synonym forconst TCHAR *
- works for both MBC and Unicode builds).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.As has been mentioned by others, you need to use
ReleaseBuffer
afterGetBuffer
. You don't need to do that for the conversion toconst char *
.@OP:
>>>> 我想我们只是担心内存泄漏或任何...
嗨,调用 GetBuffer 方法不会导致任何内存泄漏。 因为析构函数无论如何都会释放缓冲区。 但是,其他人已经警告您调用此方法的潜在问题。
@Can>>>> 当您调用 getbuffer 函数时,它会为您分配内存。
这种说法并不完全正确。 GetBuffer(0) 不分配任何内存。 它仅返回一个指向内部字符串缓冲区的指针,该指针可用于直接从 CString 类“外部”操作字符串。
但是,如果您传递一个数字,如 GetBuffer(N) 那样向其传递 N,并且如果 N 大于缓冲区的当前长度,则该函数会通过分配更多内存来确保返回的缓冲区至少与 N 一样大。
干杯,
拉杰什。
MVP,视觉++。
@ the OP:
>>> I Guess we are just worried about memory leaks or any ...
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.
@Can >>> when you call the getbuffer function it allocates memory for you.
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.
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.
Cheers,
Rajesh.
MVP, Visual ++.
当您调用 getbuffer 函数时,它会为您分配内存。
当你使用完它后,你需要调用releasebuffer来释放它
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
请尝试 http://msdn.microsoft.com/en-us/ 上的文档Library/awkwbzyc.aspx 寻求帮助。
try the documentation at http://msdn.microsoft.com/en-us/library/awkwbzyc.aspx for help on that.