如何修复strlen seg故障

How to fix strlen seg fault

本文关键字:故障 seg strlen 何修复      更新时间:2023-10-16

我正在尝试获取我的哈希功能的键的字符串长度,并且我在我尝试使用strlen的位置不断收到GDB中的此错误:

"_strlen_sse2 () at ../sysdeps/x86_64/multiarch/../strlen.S:79
79  ../sysdeps/x86_64/multiarch/../strlen.S: No such file or directory."

我尝试过运行GDB,我不允许使用字符串类,我知道我的程序不会超越此代码。

int table::hashfunc(char key[])
{       
     int hash = 0;
      int k = 1;      
     int key_size = strlen(key); //PROGRAM DOESN'T RUN PAST HERE
      for(int i = 0; i < key_size; ++i)
      {       
              hash = hash + (int)key[i];
              hash = pow(hash, k);
      }
      int index = hash % SIZE;
      return index;

 }
 //This is where I call the hash function from.
int table::add_name(char character[], char movie[], char choice)
{ 
    int index = hashfunc(character);

我希望键_size设置为键中的字符数,但是当我检查GDB中的strlen(key(时,我会得到:

 (gdb) print strlen(key)
 $1 = (size_t (*)(const char *)) 0x7ffff7156620 <__strlen_sse2>

这是我在char密钥中阅读的地方:

do  
     {
    cout << "Please enter the name of the film you 
would like to add to: " <<endl;
                            cin.get(temp_movie, temp_size);
                            cin.ignore(100, 'n');
                            cout << "Enter 'C' to add a character, 
enter 'A' to add an actor: " <<endl;
                            cin >> choice;
                            cin.ignore(100, 'n');
                            choice = toupper(choice);
                            cout << "Enter the name of the character or 
 actor you would like to add: " <<endl;
                            cin.get(temp_name, temp_size);
                            cin.ignore(100, 'n');
                            if(choice == 'C')    
                                    character_table.add_name(temp_name, 
 temp_movie, choice);
                            else if(choice == 'A')
                                    actor_table.add_name(temp_name, 
  temp_movie, choice);
                            else
                                    cout << "That is an invalid choice. 
 please try again." <<endl;

                    }while(choice != 'A' && choice != 'C');

strlen文档说:

返回给定字节字符串的长度,即 字符数组中的字符,其第一个元素被指向 到达,不包括第一个空字符。行为是 未定义,如果字符阵列指向的字符阵列中没有空字符 到str。

大概您的char key[]缺少结尾null char