在 c++ 中,如果我通过引用将 CString 传递给函数并将其分配给另一个 CString,它将被截断
一些简单的代码在我一直使用的 SDK 中开始失败,而且它显然已经正常工作了很长一段时间,事实上,我几乎肯定我已经编译了这样的部分代码并且它们已经工作了,但是最近都失败了。
在调试器中确认值的示例:
void SomeFunction(CString& paramstring) //let's pretend we pass a string with the value "hello"
{
int size=paramstring.GetLength(); //size now contains an incorrect shorter value like 4
CString localstring=paramstring;//localstring contains something like "hell" or "hel"
}
任何可能发生这种情况的想法
some simple code has begun failing in an sdk I've been working with, also it apparently has been working correctly for a long time, and indeed I'm almost positive I have compiled parts of the code like this and they have worked, but recently have failed.
example with values confirmed in debugger:
void SomeFunction(CString& paramstring) //let's pretend we pass a string with the value "hello"
{
int size=paramstring.GetLength(); //size now contains an incorrect shorter value like 4
CString localstring=paramstring;//localstring contains something like "hell" or "hel"
}
any ideas why this might be happening
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我过去在 CString 代码中犯了一个错误,当作为参数传递时,CStrings 无法正常工作。您的问题可能完全不同,但我将在这里解释我做了什么导致了这个问题,因为它可能会对某人有所帮助。
我的 CString 包含一个目录名称。为了稍微清理一下,我检查了最后一个字符是否是“\”,如果是,我通过直接覆盖 CString 内的字符来将其替换为“\0”。
这可能以某种方式破坏了 CString 的完整性,因为它在通过 strlen() 等方法测量的字符串长度与缓冲区的实际大小之间产生了不匹配。我对问题的解释可能不正确,但我认为它基本上可以归结为我破坏了 CString 的完整性。
我通过用类似的内容替换我的陈述来解决我的问题
目录名=目录名.Left(目录名.GetLength()-1);
我是凭记忆写的,并不经常使用这些函数,所以如果我记错了函数名称,请原谅我。无论如何,我的建议是不要将空字符写入 CString 的中间以缩短它,并且如果 CString 作为参数出现问题,请查找该字符(除其他外)。
啊,是的,我认为问题可能与你的相反。如果通过覆盖字符来缩短字符串,那么 GetLength() 可能会返回旧值,该值比您预期的要大。
我的实际问题是 dir + "\" + 文件名不起作用。我认为它是连接包含空值的字符串,最终值只是目录部分,因为那是添加空值的地方。
I have made a mistake in CString code in the past which stopped CStrings working correctly when passed as parameters. Your problem might be completely different, but I will explain here what I did to cause the problem, as it might help someone.
My CString contained a directory name. In order to clean it up a little, I checked if the last character was '\', and if so I replaced it with '\0' by directly overwriting the character inside the CString.
That probably somehow destroyed the integrity of the CString by creating a mismatch between the length of the string as measured by things like strlen(), and the actual size of the buffer. My explanation of the problem might be not be correct, but it basically boiled down to me corrupting the integrity of the CString, I assume.
I fixed my problem by replacing my statement with something like
dirname=dirname.Left(dirname.GetLength()-1);
I am writing this from memory and don't use the functions often, so forgive me if I have remembered a function name incorreclty. Anyway, my recommendation is not to write null characters into the middle of a CString to shorten it, and to look for that (among other things) if a problem occurs with CStrings as parameters.
Ah yes, I think the problem might be the reverse of yours. If you shorten a string by overwriting a character then maybe GetLength() will return the old value, which is larger than than you might expect.
My actual problem was that dir + "\" + filename was not working. I suppose it was concatentating strings containing a null and that the final value was just the directory part, as that is where the null had been added.
我唯一的猜测是你的内存覆盖破坏了该对象。
是时候拿出电动工具了。安装 Purify 或 Boundschecker 并找出错误。
My only guess is you have a memory overwrite corrupting that object.
Time to bring out the power tools. Install either Purify or Boundschecker and hunt your bug down.
我在控制台应用程序中尝试了您的代码,它工作得很好,问题来自代码的另一部分,这里是代码尝试(使用 MFC )
输出:
ive tried your code in a console application and it works perfectly the problem comes from another part of your code here is the the code try it out (use MFC )
output: