问题描述
我有以下代码:
#include< iostream>
#include< cstdio>
#include< list>
#include< stack>
#include< queue>
#include< set>
#include< map>
#include< unordered_set>
#include< unordered_map>
#include< limits>
#include< functional>
#include< algorithm>
#include< cmath>
#include< string>
#include< ostream>
#include< sstream>
#include< bitset>
#include< numeric>
#include< fstream>
使用namespace std;
char str [] =this.is.a.test;
char str2 [] =this.is.another.test;
typedef struct
{
size_t count;
char ** strings;
}令牌;
令牌令牌(char * String,char Split)
{
令牌t;
t.count = 1;
(size_t i = 0; String [i]!= 0; i ++)
{
if(String [i] == Split)
t。数++
}
t.strings =(char **)malloc(sizeof(char *)* t.count);
if(t.count> 0)
t.strings [0] = String;
for(size_t i = 0,j = 1; String [i]!= 0; i ++)
{
if(String [i] == Split)
{
t.strings [j] =& String [i + 1];
String [i] = 0;
j ++;
}
}
return t;
}
int main(void)
{
令牌t = Tokenize(str,'。');
printf(number of strings:%i\\\
--- \\\
,t.count);
for(size_t i = 0; i {
printf(%i:%s\\\
,i,t.strings [i] );
}
free(t.strings);
}
问题是当我调试代码,特别是那行 t.strings [j] =& String [i + 1];
在这个测试案例中.is.a.test
在第一个找到的点。 ,它应该指向这一点,但在调试器中它显示如下图。
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ the the the the the the the the the the the the the the the the the the the the the调试器显示在第55行是正确的。分配已经作出,所以在点之后 t.strings [j] 点。
请注意,在 Tokenize 中,您在堆栈上分配 Tokens t; ,然后返回此 t 。那是坏的(非常糟糕!)因为 t 在堆栈上,它将被调用 printf 覆盖。
(虽然大多数是C,但是正式地是C中的C,您不能在中声明一个初始化中的变量,如 for(size_t i = 0; )
I have the following code:
#include <iostream> #include <cstdio> #include <list> #include <stack> #include <queue> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <limits> #include <functional> #include <algorithm> #include <cmath> #include <string> #include <ostream> #include <sstream> #include <bitset> #include <numeric> #include<fstream> using namespace std; char str[] = "this.is.a.test"; char str2[] = "this.is.another.test"; typedef struct { size_t count; char** strings; } Tokens; Tokens Tokenize(char* String, char Split) { Tokens t; t.count = 1; for (size_t i = 0; String[i] != 0; i++) { if (String[i] == Split) t.count++; } t.strings =(char**) malloc(sizeof(char*)* t.count); if (t.count > 0) t.strings[0] = String; for (size_t i = 0, j = 1; String[i] != 0; i++) { if (String[i] == Split) { t.strings[j] = &String[i + 1]; String[i] = 0; j++; } } return t; } int main(void) { Tokens t = Tokenize(str, '.'); printf("number of strings: %i\n---\n", t.count); for (size_t i = 0; i < t.count; i++) { printf("%i: %s\n", i, t.strings[i]); } free(t.strings); }
The problem is when I debug the code and especially that line t.strings[j] = &String[i + 1];
In a test case of this.is.a.test
At the first found dot . , it should points to this, but in the debugger it shows the following picture. enter code here
What the debugger shows is correct at line 55. The assignment has been made, so t.strings[j] points after the dot.
Note that in Tokenize you allocate Tokens t; on the stack and later return this t. That is bad (very bad!). Because t is on the stack, it will be overwritten by the call to printf.
(And although most is C, formally it is C++ as in C you cannot declare a variable in the for initialization, as in for (size_t i = 0;)
这篇关于调试器显示错误的指针值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!