C++ LPTSTR 到 int (但是使用 atoi 存在内存覆盖问题)

发布于 2024-08-31 04:34:11 字数 1053 浏览 5 评论 0原文

我有以下代码,m_edit是一个MFC CEdit(我知道我永远不会使用MFC,但项目需要它)。

这是一个简单的循环,从文本编辑中获取文本,在获取第一行后将其转换为整数,然后将其存储在 m_y 向量中。

LPTSTR szTemp;
vector<int> m_y;
for(int i = 0; i < m_edit->GetLineCount(); i++){
  szTemp = s_y.GetBuffer(0); 
  m_edit->GetLine(i, szTemp); // get line text store in szTemp
  y = atoi(szTemp);
  m_y.push_back(y);
  szTemp = "";
  y = 0;
 }

重要示例:因此,假设 CEdit 有 6 个数字:

  • 0
  • 5
  • 2
  • 5
  • 18
  • 6

如果您使用 Visual Studio 的调试器,您会注意到异常! 它显示如下:

  • y = 0
  • y = 5
  • y = 2
  • y = 5
  • y = 18
  • y = 68

你看到了吗? szTemp 当插入 atoi 时,它返回数字 6,但连接了最后一个数字的第 2 位!这就是我执行 szTemp = ""; 的原因,但问题仍然存在。另外,假设最后一个数字是 17(不是 18),那么这次调试器会说 y = 67,所以肯定是这个问题。

但是,在 Visual Studio 调试器中,当您在此迭代期间将鼠标悬停在 szTemp 上时,它会在 szTemp 内显示“6”<--- 而不是“68”。所以不知何故 atoi 正在毁掉它。

我是否应该在将 \0 放入 atoi 之前将其连接到 szTemp 中?我如何轻松解决这个问题?

I have the following code, m_edit is a MFC CEdit (I know I would never use MFC but project demanded it).

It's a simple loop, that gets the text from a text edit, converts it to integer after getting the first line, then stores it in m_y vector.

LPTSTR szTemp;
vector<int> m_y;
for(int i = 0; i < m_edit->GetLineCount(); i++){
  szTemp = s_y.GetBuffer(0); 
  m_edit->GetLine(i, szTemp); // get line text store in szTemp
  y = atoi(szTemp);
  m_y.push_back(y);
  szTemp = "";
  y = 0;
 }

IMPORTANT EXAMPLE: So let's say the CEdit has 6 numbers:

  • 0
  • 5
  • 2
  • 5
  • 18
  • 6

If you use Visual Studio's debugger you will notice an anomaly!!
Here's what it shows:

  • y = 0
  • y = 5
  • y = 2
  • y = 5
  • y = 18
  • y = 68

Do you see that? szTemp when inserted into atoi, it returns the number 6, but concatenates the 2nd digit of the last number!!! This is why I did szTemp = "";, but the problem persists. Also, let's say the last number was 17 (not 18), then this time debugger would say y = 67, so it is definitely this problem.

However, Visual Studio debugger, when you hover over szTemp during this iteration, it says '6' <--- not '68' inside szTemp. So somehow atoi is ruining it.

Am I suppose to concatenate a \0 into szTemp before putting it into atoi? How do I solve this easily?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失与倦" 2024-09-07 04:34:12

来自 MFC CEdit::GetLine 文档:

备注:
复制的行不包含空终止字符。

因此,您需要注意 GetLine 的返回值来确定有多少字节被复制到缓冲区中,然后添加您自己的 NUL 终止符。

另外,我建议您传入缓冲区大小,以确保没有潜在的缓冲区溢出。

From the MFC CEdit::GetLine documentation:

Remarks:
The copied line does not contain a null-termination character.

So you need to pay attention to GetLine's return value to determine how many bytes were copied into the buffer and then add your own NUL-terminator.

Also, I would recommend that you pass in the buffer size to make sure you don't have a potential buffer overflow.

爱人如己 2024-09-07 04:34:12

我认为你应该为 GetBuffer 指定一个缓冲区大小

,例如

szTemp = s_y.GetBuffer(MY_MAX_STRLEN);

让 GetBuffer(0) 分配一个 0 大小的缓冲区,因为 CString(大概)是空的。

I think you should specify a buffer size for GetBuffer

e.g.

szTemp = s_y.GetBuffer(MY_MAX_STRLEN);

having GetBuffer(0) allocates a 0 sized buffer since CString is (presumably) empty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文