引发未经处理的异常:读取访问冲突。这>字符串被0x1C6F112

Unhandled exception thrown: read access violation. this->String was 0x1C6F112

本文关键字:gt 0x1C6F112 字符串 访问冲突 读取 处理 异常      更新时间:2023-10-16

因此,我正在编写一个代码,通过将字母切换到后面的字母来加密和输入,使用公式ai = letter = letter(((ai) 位置(ai) 位置(ai 1)))mod k)其中k是字母中的字母数量。

现在这是我的代码;

using namespace std;
class Encrypt {
private:
    char letters[27];
    char *String = new char[500];
    char letters_cipher[25];
    unsigned int i, k;
public:
    Encrypt() :letters{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' } {
        for (unsigned int i = 0; i < strlen(String); i++) {
            String[i] = '';
        }
    }
    ~Encrypt() {
        delete[] String;
    }
    void GetString() {
        cout << "Enter String : ";
        std::cin >> String;
    }
    void encrypt() {
        for (i = 1; i <= 26; i++) { // Run 26 times (1 for each letter)
            letters_cipher[i] = letters[(i + (i + 1)) % 26];
            cout << letters_cipher[i] << endl;
        }
        for (i = 0; i <= (strlen(String) - 1); i++) { // individual characters of string x can be referenced by x[0] etc
            for (k = 0; k <= 25; k++) {
                if (String[i] == letters[k]) {
                    cout << letters_cipher[k];
                }
            }
        }
    }
    void Print() {
        for (unsigned int i = 0; i < strlen(letters_cipher); i++) {
            cout << letters_cipher[i];
        }
    }
};

我得到以下错误

抛出的例外:阅读访问违规。 这个 ->字符串为0x128f112。

对线:

if(string [i] == letters [k])

任何想法我如何解决此问题?

编辑:

我现在已经对代码进行了一些编辑,看起来如下;

class Encrypt {
private:
    char letters[27];
    char *String = new char[500];
    char letters_cipher[27];
    unsigned int i, k;
public:
    Encrypt() :letters{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' } {
        for (unsigned int i = 0; i < 500; i++) {
            String[i] = '';
        }
    }
    ~Encrypt() {
        delete[] String;
    }
    void GetString() {
        cout << "Enter String : ";
        std::cin >> String;
    }
    void encrypt() {
        for (i = 1; i <= 26; i++) { // Run 26 times (1 for each letter)
            letters_cipher[i] = letters[(i + (i + 1)) % 26];
            cout << letters_cipher[i] << endl;
        }
        for (i = 0; i <= (sizeof(String) - 1); i++) { // individual characters of string x can be referenced by x[0] etc
            for (k = 0; k <= 25; k++) {
                if (String[i] == letters[k]) {
                    cout << letters_cipher[k];
                }
            }
        }
    }
    void Print() {
        for (unsigned int i = 0; i < sizeof(letters_cipher); i++) {
            cout << letters_cipher[i];
        }
    }
};

错误不再存在,程序运行但会以错误关闭;

'consoleapplication5.exe'(win32):加载'c: windows syswow64 kernel.appcore.dll'。找不到或打开PDB 文件。'consoleapplication5.exe'(Win32):已加载 'c: windows syswow64 msvcrt.dll'。找不到或打开PDB文件。 'consoleapplication5.exe'(Win32):已加载 'c: windows syswow64 rpcrt4.dll'。找不到或打开PDB文件。 'consoleapplication5.exe'(Win32):已加载 'c: windows syswow64 sspicli.dll'。找不到或打开PDB文件。 'consoleapplication5.exe'(Win32):已加载 'c: windows syswow64 cryptbase.dll'。找不到或打开PDB文件。 'consoleapplication5.exe'(Win32):已加载 'c: windows syswow64 bcryptprimitimitives.dll'。找不到或打开 PDB文件。'consoleapplication5.exe'(Win32):已加载 'c: windows syswow64 sechost.dll'。找不到或打开PDB文件。 程序'[20764] consoleapplication5.exe'已使用代码0退出 (0x0)。

in

    for (i = 1; i <= 26; i++) { // Run 26 times (1 for each letter)
        letters_cipher[i] = letters[(i + (i + 1)) % 26];

您在letters_cipher中写2个索引是char letters_cipher[25];,行为是未定义的

       for (k = 0; k <= 25; k++) {
            if (String[i] == letters[k]) {
                cout << letters_cipher[k];
            }

您从letters_cipher中读取一个索引,行为再次不确定

letters_cipher中的最大允许索引为24

还请注意

   for (unsigned int i = 0; i < strlen(letters_cipher); i++) {
        cout << letters_cipher[i];
    }

strlen(letters_cipher)也无法工作,因为在您的代码中,您在其中输入零结尾字符的任何地方都没有任何地方,因此 strlen 也将脱离letters_cipher的初始化部分,并且可能会再次出现。

您也有一个问题:

    for (unsigned int i = 0; i < strlen(String); i++) {
        String[i] = '';
    }

因为您在没有初始化的 string 的情况下进行strlen(String),请替换为500(或更好地使用std :: string)

任何想法我如何解决此问题?

一般方式,请勿在循环中使用诸如26或25的数字,使用sizeof,也可以使用 std :: vector 而不是(c)数组。当内容恒定时,当内容不尺寸时,因此对于letters,只需做:

static const char letters_cipher[] = {
   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
   'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
   'y', 'z'
};

可能的letters_cipher必须具有与letters相同的大小,并更正集合,因为for在1而不是0

开始

其他备注

  • 数组letters是没有用的,它仅包含字母A..z,因此letters[i]只是'a' + i
  • 您同时执行using namespace std;std::xxx,选择其中之一(很多人会说您不做using namespace std;

建议如果我很好地了解您的内容:

#include <iostream>
#include <string>
class Encrypt {
  private:
    std::string str;
    char letters_cipher['z' - 'a' + 1];
    inline char letter(size_t i) { return 'a' + i; } // to help
  public:
    Encrypt() {} // default definition is useless in fact
    ~Encrypt() {} // default definition is useless in fact
    bool Getstr() {
      std::cout << "Enter str : ";
     return (std::cin >> str); // to indicates EOF in case
    }
    void encrypt() {
      for (size_t i = 1; i <= sizeof(letters_cipher); i++) { // Run 26 times (1 for each letter)
        letters_cipher[i - 1] = letter((i + (i + 1)) % 26);
        std::cout << letters_cipher[i - 1] << std::endl;
      }
      for (size_t i = 0; i < str.length(); i++) { // individual characters of string x can be referenced by x[0] etc
        for (size_t k = 0; k < sizeof(letters_cipher); k++) {
          if (str[i] == letter(k)) {
            std::cout << letters_cipher[k];
          }
        }
      }
      std::cout << std::endl;
    }
    void Print() {
      for (size_t i = 0; i < sizeof(letters_cipher); i++) {
        std::cout << letters_cipher[i];
      }
      std::cout << std::endl;
    }
};
int main()
{
  Encrypt e;
  e.Getstr();
  e.encrypt();
  e.Print();
}

汇编和执行:

/tmp % g++ -pedantic -Wextra e.cc
/tmp % ./a.out
Enter str : azerty
d
f
h
j
l
n
p
r
t
v
x
z
b
d
f
h
j
l
n
p
r
t
v
x
z
b
dbllpz
dfhjlnprtvxzbdfhjlnprtvxzb

valgrind 下执行:

/tmp % valgrind ./a.out
==29157== Memcheck, a memory error detector
==29157== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==29157== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==29157== Command: ./a.out
==29157== 
Enter str : azerty
d
f
h
j
l
n
p
r
t
v
x
z
b
d
f
h
j
l
n
p
r
t
v
x
z
b
dbllpz
dfhjlnprtvxzbdfhjlnprtvxzb
==29157== 
==29157== HEAP SUMMARY:
==29157==     in use at exit: 0 bytes in 0 blocks
==29157==   total heap usage: 4 allocs, 4 frees, 115 bytes allocated
==29157== 
==29157== All heap blocks were freed -- no leaks are possible
==29157== 
==29157== For counts of detected and suppressed errors, rerun with: -v
==29157== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)