C++ - strcmp()无法正常工作?

Onu*_*glu 4 c++ strcmp

有一些非常奇怪的事情:虽然两个字符串完全相同,但strcmp()返回-1.以下是调试器输出(gdb)的片段:

(gdb) print s[i][0] == grammar->symbols_from_int[107][0]
$36 = true
(gdb) print s[i][1] == grammar->symbols_from_int[107][1]
$37 = true
(gdb) print s[i][2] == grammar->symbols_from_int[107][2]
$38 = true
(gdb) print s[i][3] == grammar->symbols_from_int[107][3]
$39 = true
(gdb) print s[i][4] == grammar->symbols_from_int[107][4]
$40 = true
(gdb) print s[i][5] == grammar->symbols_from_int[107][5]
$41 = false
(gdb) print grammar->symbols_from_int[107][4]
$42 = 0 '\0'
(gdb) print s[i]
$43 = (char * const&) @0x202dc50: 0x202d730 "Does"
(gdb) print grammar->symbols_from_int[107]
$44 = (char * const&) @0x1c9fb08: 0x1c9a062 "Does"
(gdb) print strcmp(s[i],grammar->symbols_from_int[107])
$45 = -1
Run Code Online (Sandbox Code Playgroud)

知道发生了什么事吗?

提前致谢,

厄尼尔

编辑1:以下是我的代码的一些片段:

# include <unordered_map>       // Used as hash table
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <vector>

using namespace std;
using std::unordered_map;
using std::hash;

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};

...
<some other code>
...

class BPCFG {

  public:

        char *symbols;  // Character array holding all grammar symbols, with NULL seperating them.
        char *rules;    // Character array holding all rules, with NULL seperating them.

        unordered_map<char *, int , hash<char *> , eqstr> int_from_symbols; // Hash table holding the grammar symbols and their integer indices as key/value pairs.
...
<some other code>
...

vector<char *> symbols_from_int;        // Hash table holding the integer indices and their corresponding grammar symbols as key/value pairs.
void load_symbols_from_file(const char *symbols_file);
}

void BPCFG::load_symbols_from_file(const char *symbols_file) {
        char buffer[200];
        FILE *input = fopen(symbols_file, "r");
        int symbol_index = 0;
        while(fscanf(input, "%s", buffer) > 0) {
                if(buffer[0] == '/')
                        strcpy(symbols + symbol_index, buffer+1);
                else
                        strcpy(symbols + symbol_index, buffer);
                symbols_from_int.push_back(symbols + symbol_index);
                int_from_symbols[symbols+symbol_index] = symbols_from_int.size()-1;
                probs.push_back(vector<double>());
                hyperprobs.push_back(vector<double>());
                rules_from_IntPair.push_back(vector<char *>());
                symbol_index += strlen(symbols+symbol_index) + 1;
        }


        fclose(input);
}
Run Code Online (Sandbox Code Playgroud)

最后一个函数(BPCFG :: load_symbols_from_file)似乎是我在整个代码中修改symbols_from_int的唯一函数.如果您需要更多代码,请告诉我.我不会放一切,因为它有数百行.

编辑2:好的,我想我应该从我的代码中再添加一个东西.这是BPCFG类的构造函数:

BPCFG(int symbols_length, int rules_length, int symbol_count, int rule_count):
   int_from_symbols(1.5*symbol_count),
   IntPair_from_rules(1.5*rule_count),
   symbol_after_dot(10*rule_count)
{
    symbols = (char *)malloc(symbols_length*sizeof(char));
    rules = (char *)malloc(rules_length*sizeof(char));
}
Run Code Online (Sandbox Code Playgroud)

编辑3:这是错误点路径上的代码.它不是可编译的,但是它显示了代码所在的位置(我在调试器中检查了next和step命令,代码确实遵循了这条路径):

BPCFG my_grammar(2000, 5500, 194, 187);
my_grammar.load_symbols_from_file("random_50_1_words_symbols.txt");
<some irrelevant code>
my_grammar.load_rules_from_file("random_50_1_words_grammar.txt", true);
<some irrelevant code>
my_grammar.load_symbols_after_dots();

BPCFGParser my_parser(&my_grammar);
BPCFGParser::Sentence s;

// (Sentence is defined in the BPCFGParser class with
// typedef vector<char *> Sentence;)

Edge e;
try {
        my_parser.parse(s, e);
}
catch(char *e) {fprintf(stderr, "%s", e);}

void BPCFGParser::parse(const Sentence & s, Edge & goal_edge) {

        /* Initializing the chart */

        chart::active_sets.clear();
        chart::passive_sets.clear();
        chart::active_sets.resize(s.size());
        chart::passive_sets.resize(s.size());

        // initialize(sentence, goal);

        try {
                initialize(s, goal_edge);
        }
        catch (char *e) {
                if(strcmp(e, UNKNOWN_WORD) == 0)
                        throw e;
        }
<Does something more, but the execution does not come to this point>
}

void BPCFGParser::initialize(const Sentence & s, Edge & goal_edge) {
        // create a new chart and new agendas
        /* For now, we plan to do this during constructing the BPCFGParser object */

        // for each word w:[start,end] in the sentence
        //   discoverEdge(w:[start,end])

        Edge temp_edge;

        for(int i = 0;i < s.size();i++) {
                temp_edge.span.start = i;
                temp_edge.span.end = i+1;
                temp_edge.isActive = false;
                /* Checking whether the given word is ever seen in the training corpus */
                unordered_map<char *, int , hash<char *> , eqstr>::const_iterator it = grammar->int_from_symbols.find(s[i]);
                if(it == grammar->int_from_symbols.end())
                        throw UNKNOWN_WORD;
                <Does something more, but execution does not come to this point>
        }
}
Run Code Online (Sandbox Code Playgroud)

我在调试器中运行print命令的地方是最后一个

throw UNKNOWN_WORD;
Run Code Online (Sandbox Code Playgroud)

命令.我的意思是,我在GDB上接下来,看到这一行后,我运行了所有这些打印命令.

感谢您的关注,
Onur


好的,我想我应该从我的代码中添加一件事.这是BPCFG类的构造函数:

BPCFG(int symbols_length, int rules_length, int symbol_count, int rule_count):
   int_from_symbols(1.5*symbol_count),
   IntPair_from_rules(1.5*rule_count),
   symbol_after_dot(10*rule_count)
{
    symbols = (char *)malloc(symbols_length*sizeof(char));
    rules = (char *)malloc(rules_length*sizeof(char));
}
Run Code Online (Sandbox Code Playgroud)

epa*_*tel 10

这听起来像是s指向堆栈上的数组的指针,一旦调用了新函数,它就会被覆盖,即strcmp()

strcmp()调用之后,调试器会说它们是什么?

  • +1.是的.数据损坏的可能性比strcmp中的错误高出一百万倍!使用调试器检查字符串时,字符串显然是相同的,但这并不意味着strcmp执行时内存位置仍然具有这些值.(@Onur:发布失败的实际代码会使一切变得更加清晰). (2认同)

Tho*_*ini 1

鉴于 GDB 输出,我能看到的唯一可能的原因是 strcmp() 被窃听。

您基本上在 GDB 中做了 strcmp 所做的事情:逐个字符进行比较,直到两者都为零(在 4 处)。

你能试一下print strcmp("Does", "Does");吗 ?


编辑:也尝试:

print stricmp(s[i], grammar->symbols_from_int[107], 1);
print stricmp(s[i], grammar->symbols_from_int[107], 2);
print stricmp(s[i], grammar->symbols_from_int[107], 3);
print stricmp(s[i], grammar->symbols_from_int[107], 4);
print stricmp(s[i], grammar->symbols_from_int[107], 5);
Run Code Online (Sandbox Code Playgroud)

  • strcmp 已经流行很长时间了。提问者所做的事情很可能与他/她的期望不符。 (8认同)
  • 一个非常常用的库中的错误,尤其是做这么琐碎的事情,是非常不可能的。除非他从标准库以外的地方获取 strcmp。 (3认同)
  • 澄清一下,我没有写我的 strcmp()。我正在使用 string.h 中的那个 (2认同)