将静态字符数组中的字符分配给动态分配的字符数组 – 访问冲突

huangapple go评论74阅读模式
英文:

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 &lt;iostream&gt;
char* Encode(unsigned long long int);
unsigned long long int Decode(const char*);
int main() {
try {
setlocale(LC_CTYPE, &quot;rus&quot;);
unsigned long long int Input = 1223212123412142;
std::cout &lt;&lt; Input &lt;&lt; std::endl;
char* EncodedStr = Encode(Input);
std::cout &lt;&lt; EncodedStr &lt;&lt; std::endl;
unsigned long long int Output = Decode(EncodedStr);
std::cout &lt;&lt; Output &lt;&lt; std::endl;
} catch(std::exception e) {
std::cout &lt;&lt; &quot;Exception has been thrown! Exception&quot; &lt;&lt; e.what() &lt;&lt; std::endl;
}
std::cin.get();
}
char WordMap[] = {&#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39;, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;, &#39;а&#39;, &#39;б&#39;, &#39;в&#39;, &#39;г&#39;, &#39;д&#39;, &#39;е&#39;, &#39;ё&#39;, &#39;ж&#39;, &#39;з&#39;, &#39;и&#39;, &#39;й&#39;, &#39;к&#39;, &#39;л&#39;, &#39;м&#39;, &#39;н&#39;, &#39;о&#39;, &#39;п&#39;, &#39;р&#39;, &#39;с&#39;, &#39;т&#39;, &#39;у&#39;, &#39;ф&#39;, &#39;х&#39;, &#39;ц&#39;, &#39;ч&#39;, &#39;ш&#39;, &#39;щ&#39;, &#39;ь&#39;, &#39;ы&#39;, &#39;ь&#39;, &#39;э&#39;, &#39;ю&#39;, &#39;я&#39;, &#39;А&#39;, &#39;Б&#39;, &#39;В&#39;, &#39;Г&#39;, &#39;Д&#39;, &#39;Е&#39;, &#39;Ё&#39;, &#39;Ж&#39;, &#39;З&#39;, &#39;И&#39;, &#39;Й&#39;, &#39;К&#39;, &#39;Л&#39;, &#39;М&#39;, &#39;Н&#39;, &#39;О&#39;, &#39;П&#39;, &#39;Р&#39;, &#39;С&#39;, &#39;Т&#39;, &#39;У&#39;, &#39;Ф&#39;, &#39;Х&#39;, &#39;Ц&#39;, &#39;Ч&#39;, &#39;Ш&#39;, &#39;Щ&#39;, &#39;Ь&#39;, &#39;Ы&#39;, &#39;Ь&#39;, &#39;Э&#39;, &#39;Ю&#39;, &#39;Я&#39;};
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 &gt; pow(WordAmount, I)) {
I += 1;
}
int Size = I + 1;
char* Encoded = new char[Size];
std::cout &lt;&lt; I &lt;&lt; std::endl;
while(I &gt;= 0) {
D = pow(WordAmount, I);
std::cout &lt;&lt; I &lt;&lt; std::endl;
try{
Encoded[Size - I - 1] = WordMap[Previous / D]; // Here&#39;s the exception being thrown. Notice it&#39;s only when I &lt; 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&#39;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 &gt; 0) {
for(size_t i = 0; i &lt; 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 &quot;pch.h&quot;
#include &lt;iostream&gt;
int Encode(unsigned long long int, char**);
unsigned long long int Decode(const char*, int);
using namespace std;
int main() {
setlocale(LC_CTYPE, &quot;rus&quot;);
unsigned long long int Input = 0;
unsigned long long int Output = 0;
char* EncodedStr = NULL;
int Size = 0;
while(true) {
cin &gt;&gt; Input;
Size = Encode(Input, &amp;EncodedStr);
Output = Decode(EncodedStr, Size);
cout &lt;&lt; EncodedStr &lt;&lt; endl;
cout &lt;&lt; Output &lt;&lt; endl;
}
}
const char WordMap[] = {&#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39;, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;, &#39;а&#39;, &#39;б&#39;, &#39;в&#39;, &#39;г&#39;, &#39;д&#39;, &#39;е&#39;, &#39;ё&#39;, &#39;ж&#39;, &#39;з&#39;, &#39;и&#39;, &#39;й&#39;, &#39;к&#39;, &#39;л&#39;, &#39;м&#39;, &#39;н&#39;, &#39;о&#39;, &#39;п&#39;, &#39;р&#39;, &#39;с&#39;, &#39;т&#39;, &#39;у&#39;, &#39;ф&#39;, &#39;х&#39;, &#39;ц&#39;, &#39;ч&#39;, &#39;ш&#39;, &#39;щ&#39;, &#39;ь&#39;, &#39;ы&#39;, &#39;ь&#39;, &#39;э&#39;, &#39;ю&#39;, &#39;я&#39;, &#39;А&#39;, &#39;Б&#39;, &#39;В&#39;, &#39;Г&#39;, &#39;Д&#39;, &#39;Е&#39;, &#39;Ё&#39;, &#39;Ж&#39;, &#39;З&#39;, &#39;И&#39;, &#39;Й&#39;, &#39;К&#39;, &#39;Л&#39;, &#39;М&#39;, &#39;Н&#39;, &#39;О&#39;, &#39;П&#39;, &#39;Р&#39;, &#39;С&#39;, &#39;Т&#39;, &#39;У&#39;, &#39;Ф&#39;, &#39;Х&#39;, &#39;Ц&#39;, &#39;Ч&#39;, &#39;Ш&#39;, &#39;Щ&#39;, &#39;Ь&#39;, &#39;Ы&#39;, &#39;Ь&#39;, &#39;Э&#39;, &#39;Ю&#39;, &#39;Я&#39;};
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 &gt; pow(WordAmount, I + 1)) {
I += 1;
}
const int Size = I + 1;
*Output = new char[Size + 1];
(*Output)[Size] = 0;
while(I &gt;= 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 &gt;= 0) {
for(size_t i = 0; i &lt; 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;
}

huangapple
  • 本文由 发表于 2020年1月4日 12:07:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定