引发未经处理的异常:读取访问冲突。这>字符串被0x1C6F112
Unhandled exception thrown: read access violation. this->String was 0x1C6F112
因此,我正在编写一个代码,通过将字母切换到后面的字母来加密和输入,使用公式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)
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- 引发未经处理的异常:读取访问冲突。这>字符串被0x1C6F112
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 使用shared_ptr<字符串>转换为一个无序集合<字符串>
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾