英文:
Assigning chars from static char array to dynamically allocated char array - Access violation
问题
以下是您提供的代码的翻译部分:
我是C++的新手,正在尝试一些有趣的事情。但我不明白为什么这段代码不起作用,而且抛出了访问内存违规异常。
它能工作,但当I等于6时,它会抛出异常。
我在这里做的是尝试使用128位位置数字系统进行实验。
代码 -
#include "pch.h"
#include <iostream>
char* Encode(unsigned long long int);
unsigned long long int Decode(const char*);
int main() {
try {
setlocale(LC_CTYPE, "rus");
unsigned long long int Input = 1223212123412142;
std::cout << Input << std::endl;
char* EncodedStr = Encode(Input);
std::cout << EncodedStr << std::endl;
unsigned long long int Output = Decode(EncodedStr);
std::cout << Output << std::endl;
} catch(std::exception e) {
std::cout << "发生异常!异常信息:" << e.what() << std::endl;
}
std::cin.get();
}
char WordMap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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', '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', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ы', 'ь', 'э', 'ю', 'я', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ь', 'Ы', 'Ь', 'Э', 'Ю', 'Я'};
const int WordAmount = sizeof(WordMap) / sizeof(char);
char* Encode(unsigned long long int Input) {
int I = 0;
unsigned long long int D = 0;
unsigned long long int Previous = Input;
while(Input > pow(WordAmount, I)) {
I += 1;
}
int Size = I + 1;
char* Encoded = new char[Size];
std::cout << I << std::endl;
while(I >= 0) {
D = pow(WordAmount, I);
std::cout << I << std::endl;
try{
Encoded[Size - I - 1] = WordMap[Previous / D]; // 这里抛出异常。请注意,仅当I < 8时,才会抛出异常,8是此输入中的幂的数量。这也意味着Encoded[0]和可能从0到3都被赋值,而从3到8则没有。
} catch(...) {
}
Previous -= D;
I -= 1;
}
return Encoded;
}
unsigned long long int Decode(const char* Input) {
int I = strlen(Input);
int K = 0;
unsigned long long int Previous = 0;
while(I > 0) {
for(size_t i = 0; i < WordAmount; i++) {
if(WordMap[i] == Input[I - 1]) {
K = i;
break;
}
}
Previous += K * (unsigned long long int)pow(WordAmount, I - 1);
I -= 1;
}
return Previous;
}
希望这有助于您理解代码。如果您有任何其他问题,请随时提出。
英文:
I'm new to C++, having some fun with it. And I don't understand why this code isn't working and throwing access memory violation exception.
It works but when I is 6 it throws the exception
What I'm doing here is I'm trying to experiment with 128 position numeral system.
Code -
#include <iostream>
char* Encode(unsigned long long int);
unsigned long long int Decode(const char*);
int main() {
try {
setlocale(LC_CTYPE, "rus");
unsigned long long int Input = 1223212123412142;
std::cout << Input << std::endl;
char* EncodedStr = Encode(Input);
std::cout << EncodedStr << std::endl;
unsigned long long int Output = Decode(EncodedStr);
std::cout << Output << std::endl;
} catch(std::exception e) {
std::cout << "Exception has been thrown! Exception" << e.what() << std::endl;
}
std::cin.get();
}
char WordMap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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', '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', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ы', 'ь', 'э', 'ю', 'я', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ь', 'Ы', 'Ь', 'Э', 'Ю', 'Я'};
const int WordAmount = sizeof(WordMap) / sizeof(char);
char* Encode(unsigned long long int Input) {
int I = 0;
unsigned long long int D = 0;
unsigned long long int Previous = Input;
while(Input > pow(WordAmount, I)) {
I += 1;
}
int Size = I + 1;
char* Encoded = new char[Size];
std::cout << I << std::endl;
while(I >= 0) {
D = pow(WordAmount, I);
std::cout << I << std::endl;
try{
Encoded[Size - I - 1] = WordMap[Previous / D]; // Here's the exception being thrown. Notice it's only when I < 8, 8 is number of powers in this Input. It also means that Encoded[0] and probably from 0 to 3 are being assigned and from 3 to 8 aren't
} catch(...) {
}
Previous -= D;
I -= 1;
}
return Encoded;
}
unsigned long long int Decode(const char* Input) {
int I = strlen(Input);
int K = 0;
unsigned long long int Previous = 0;
while(I > 0) {
for(size_t i = 0; i < WordAmount; i++) {
if(WordMap[i] == Input[I - 1]) {
K = i;
break;
}
}
Previous += K * (unsigned long long int)pow(WordAmount, I - 1);
I -= 1;
}
return Previous;
}
答案1
得分: 0
修复了代码,现在它可以正常工作了。问题出在算法中。
#include <iostream>
int Encode(unsigned long long int, char**);
unsigned long long int Decode(const char*, int);
using namespace std;
int main() {
setlocale(LC_CTYPE, "rus");
unsigned long long int Input = 0;
unsigned long long int Output = 0;
char* EncodedStr = NULL;
int Size = 0;
while(true) {
cin >> Input;
Size = Encode(Input, &EncodedStr);
Output = Decode(EncodedStr, Size);
cout << EncodedStr << endl;
cout << Output << endl;
}
}
const char WordMap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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', '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', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ы', 'ь', 'э', 'ю', 'я', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ь', 'Ы', 'Ь', 'Э', 'Ю', 'Я'};
const int WordAmount = sizeof(WordMap) / sizeof(char);
int Encode(unsigned long long int Input, char** Output) {
int I = 0;
int K = 0;
unsigned long long int D = 0;
unsigned long long int Previous = Input;
while(Input > pow(WordAmount, I + 1)) {
I += 1;
}
const int Size = I + 1;
*Output = new char[Size + 1];
(*Output)[Size] = 0;
while(I >= 0) {
D = powl(WordAmount, I);
K = Previous / D;
(*Output)[Size - I - 1] = WordMap[K];
Previous -= D * K;
I -= 1;
}
return Size;
}
unsigned long long int Decode(const char* Input, int Size) {
int I = Size - 1;
int K = 0;
unsigned long long int Previous = 0;
while(I >= 0) {
for(size_t i = 0; i < WordAmount; i++) {
if(WordMap[i] == Input[Size - I - 1]) {
K = i;
break;
}
}
Previous += K * (unsigned long long int)powl(WordAmount, I);
I -= 1;
}
return Previous;
}
这是已修复的代码。
英文:
Fixed the code. It works now. The problem was in algorithm
#include "pch.h"
#include <iostream>
int Encode(unsigned long long int, char**);
unsigned long long int Decode(const char*, int);
using namespace std;
int main() {
setlocale(LC_CTYPE, "rus");
unsigned long long int Input = 0;
unsigned long long int Output = 0;
char* EncodedStr = NULL;
int Size = 0;
while(true) {
cin >> Input;
Size = Encode(Input, &EncodedStr);
Output = Decode(EncodedStr, Size);
cout << EncodedStr << endl;
cout << Output << endl;
}
}
const char WordMap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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', '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', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ы', 'ь', 'э', 'ю', 'я', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ь', 'Ы', 'Ь', 'Э', 'Ю', 'Я'};
const int WordAmount = sizeof(WordMap) / sizeof(char);
int Encode(unsigned long long int Input, char** Output) {
int I = 0;
int K = 0;
unsigned long long int D = 0;
unsigned long long int Previous = Input;
while(Input > pow(WordAmount, I + 1)) {
I += 1;
}
const int Size = I + 1;
*Output = new char[Size + 1];
(*Output)[Size] = 0;
while(I >= 0) {
D = powl(WordAmount, I);
K = Previous / D;
(*Output)[Size - I - 1] = WordMap[K];
Previous -= D * K;
I -= 1;
}
return Size;
}
unsigned long long int Decode(const char* Input, int Size) {
int I = Size - 1;
int K = 0;
unsigned long long int Previous = 0;
while(I >= 0) {
for(size_t i = 0; i < WordAmount; i++) {
if(WordMap[i] == Input[Size - I - 1]) {
K = i;
break;
}
}
Previous += K * (unsigned long long int)powl(WordAmount, I);
I -= 1;
}
return Previous;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论