引发未处理的异常:读取访问冲突。 this-> String为0x1C6F112

时间:2019-03-06 13:00:27

标签: c++ encryption

所以我正在写一个密码,该密码通过使用公式ai = letter((position(ai)+ position(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] = '\0';
        }
    }
    ~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];
        }
    }

};

我收到以下错误

  

抛出异常:读取访问冲突。   this-> String为0x128F112。

该行:

  

if(String [i] ==字母[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] = '\0';
        }
    }
    ~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 \ bcryptprimitives.dll'。找不到或打开   PDB文件。 'ConsoleApplication5.exe'(Win32):已加载   'C:\ Windows \ SysWOW64 \ sechost.dll'。找不到或打开PDB文件。   程序“ [20764] ConsoleApplication5.exe”已退出,代码为0   (0x0)。

1 个答案:

答案 0 :(得分:4)

    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] = '\0';
    }

因为您在没有初始化 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)
相关问题