了解strtok_s [英] Understanding strtok_s

查看:82
本文介绍了了解strtok_s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++已经很久了,所以请忍受。

It's been ages I've worked with C++, so please bear with me.

我是C#开发人员,但是这个C ++控制台应用坏了,我正在管理它。我目前正在使用VS2010对其进行调试。

I'm a C# developer, but this C++ console app broke, and I'm managing it now. I'm currently debugging it with VS2010.

由于机密性和大量代码,我无法发布所有代码。

Due to confidentiality and the sheer amount of code, I cannot post all the code.

控制台实际上读取并解析6MB XML文件。一切正常,直到昨天,我收到一个带有以下代码行的访问冲突读取位置0xcccccccc:

The console essentially reads and parses a 6MB XML file. Everything used to work, until yesterday, where I receive a "Access violation reading location 0xcccccccc" with the following line of code:

strtok_s(0, "=", &token);

当我将光标放在& token上时,我看到错误 token = 0xcccccccc

When I place the cursor over "&token", I see the error "token = 0xcccccccc ".

我想澄清一些事情:

strtok_s(buf, "=", &token);
strtok_s(0, "=", &token);
strtok_s(0, "=", &token);

0 作为参数做什么?我假设在第一个* strtok_s *中,令牌将指向 buf 中char’='的第一个实例。但是在第二条指令中, 0的目的是什么?

what is the 0 doing as parameter? I assume that in the first *strtok_s*, token will point to the first instance of the char '=' in buf. But in the 2nd instruction, what's the purpose of '0'? Is it looking for '=' inside token?

很明显,XML中的某些内容发生了变化。因此,最后,考虑到这正在解析一个6MB的文件(带有870K行文本),并且读取XML的C ++函数的长度为700多行,因此,调试此错误的最佳方法是什么?不,还没有考虑要割腕...

Obviously, something has changed in the XML. So, finally, considering that this is parsing a 6MB file (w/ 870K lines of text) and the C++ function that reads the XML is 700+ lines long, what's the best way to debug this? And no, slitting my wrists is not being condidered... yet.

谢谢。

推荐答案

将0作为第一个参数传递(也许更容易读取NULL)表示在字符串中查找下一个标记。如果将0xcccccccccc作为最后一个参数的值( token是错误的名称),则表明您从未在字符串中搜索 first 标记。换句话说,从未调用过 strtok_s(buf, =。&令牌)。否则, token值未从先前对strtok_s()的调用中保留。因此,它不再知道需要搜索什么字符串。 Kaboom。

Passing 0 as the first argument (NULL is more readable perhaps) means "find the next token in the string". Seeing 0xcccccccc as the value of the last argument ("token" is the wrong name) tells you that the string was never searched for the first token. In other words, a call to strtok_s(buf, "=". &token) was never made. Or the "token" value wasn't preserved from an earlier call to strtok_s(). So it no longer knows what string needs to be searched. Kaboom.

无论哪种情况,它都是代码中的硬错误,不受字符串本身的影响。

In either case it is a hard bug in the code that isn't affected by the string itself.

这篇关于了解strtok_s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆