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

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

Assigning chars from static char array to dynamically allocated char array - Access violation

问题

以下是您提供的代码的翻译部分:

  1. 我是C++的新手,正在尝试一些有趣的事情。但我不明白为什么这段代码不起作用,而且抛出了访问内存违规异常。
  2. 它能工作,但当I等于6,它会抛出异常。
  3. 我在这里做的是尝试使用128位位置数字系统进行实验。
  4. 代码 -
  5. #include "pch.h"
  6. #include <iostream>
  7. char* Encode(unsigned long long int);
  8. unsigned long long int Decode(const char*);
  9. int main() {
  10. try {
  11. setlocale(LC_CTYPE, "rus");
  12. unsigned long long int Input = 1223212123412142;
  13. std::cout << Input << std::endl;
  14. char* EncodedStr = Encode(Input);
  15. std::cout << EncodedStr << std::endl;
  16. unsigned long long int Output = Decode(EncodedStr);
  17. std::cout << Output << std::endl;
  18. } catch(std::exception e) {
  19. std::cout << "发生异常!异常信息:" << e.what() << std::endl;
  20. }
  21. std::cin.get();
  22. }
  23. 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', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ы', 'ь', 'э', 'ю', 'я', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ь', 'Ы', 'Ь', 'Э', 'Ю', 'Я'};
  24. const int WordAmount = sizeof(WordMap) / sizeof(char);
  25. char* Encode(unsigned long long int Input) {
  26. int I = 0;
  27. unsigned long long int D = 0;
  28. unsigned long long int Previous = Input;
  29. while(Input > pow(WordAmount, I)) {
  30. I += 1;
  31. }
  32. int Size = I + 1;
  33. char* Encoded = new char[Size];
  34. std::cout << I << std::endl;
  35. while(I >= 0) {
  36. D = pow(WordAmount, I);
  37. std::cout << I << std::endl;
  38. try{
  39. Encoded[Size - I - 1] = WordMap[Previous / D]; // 这里抛出异常。请注意,仅当I < 8时,才会抛出异常,8是此输入中的幂的数量。这也意味着Encoded[0]和可能从0到3都被赋值,而从3到8则没有。
  40. } catch(...) {
  41. }
  42. Previous -= D;
  43. I -= 1;
  44. }
  45. return Encoded;
  46. }
  47. unsigned long long int Decode(const char* Input) {
  48. int I = strlen(Input);
  49. int K = 0;
  50. unsigned long long int Previous = 0;
  51. while(I > 0) {
  52. for(size_t i = 0; i < WordAmount; i++) {
  53. if(WordMap[i] == Input[I - 1]) {
  54. K = i;
  55. break;
  56. }
  57. }
  58. Previous += K * (unsigned long long int)pow(WordAmount, I - 1);
  59. I -= 1;
  60. }
  61. return Previous;
  62. }

希望这有助于您理解代码。如果您有任何其他问题,请随时提出。

英文:

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 -

  1. #include &lt;iostream&gt;
  2. char* Encode(unsigned long long int);
  3. unsigned long long int Decode(const char*);
  4. int main() {
  5. try {
  6. setlocale(LC_CTYPE, &quot;rus&quot;);
  7. unsigned long long int Input = 1223212123412142;
  8. std::cout &lt;&lt; Input &lt;&lt; std::endl;
  9. char* EncodedStr = Encode(Input);
  10. std::cout &lt;&lt; EncodedStr &lt;&lt; std::endl;
  11. unsigned long long int Output = Decode(EncodedStr);
  12. std::cout &lt;&lt; Output &lt;&lt; std::endl;
  13. } catch(std::exception e) {
  14. std::cout &lt;&lt; &quot;Exception has been thrown! Exception&quot; &lt;&lt; e.what() &lt;&lt; std::endl;
  15. }
  16. std::cin.get();
  17. }
  18. 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;};
  19. const int WordAmount = sizeof(WordMap) / sizeof(char);
  20. char* Encode(unsigned long long int Input) {
  21. int I = 0;
  22. unsigned long long int D = 0;
  23. unsigned long long int Previous = Input;
  24. while(Input &gt; pow(WordAmount, I)) {
  25. I += 1;
  26. }
  27. int Size = I + 1;
  28. char* Encoded = new char[Size];
  29. std::cout &lt;&lt; I &lt;&lt; std::endl;
  30. while(I &gt;= 0) {
  31. D = pow(WordAmount, I);
  32. std::cout &lt;&lt; I &lt;&lt; std::endl;
  33. try{
  34. 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
  35. } catch(...) {
  36. }
  37. Previous -= D;
  38. I -= 1;
  39. }
  40. return Encoded;
  41. }
  42. unsigned long long int Decode(const char* Input) {
  43. int I = strlen(Input);
  44. int K = 0;
  45. unsigned long long int Previous = 0;
  46. while(I &gt; 0) {
  47. for(size_t i = 0; i &lt; WordAmount; i++) {
  48. if(WordMap[i] == Input[I - 1]) {
  49. K = i;
  50. break;
  51. }
  52. }
  53. Previous += K * (unsigned long long int)pow(WordAmount, I - 1);
  54. I -= 1;
  55. }
  56. return Previous;
  57. }

答案1

得分: 0

修复了代码,现在它可以正常工作了。问题出在算法中。

  1. #include <iostream>
  2. int Encode(unsigned long long int, char**);
  3. unsigned long long int Decode(const char*, int);
  4. using namespace std;
  5. int main() {
  6. setlocale(LC_CTYPE, "rus");
  7. unsigned long long int Input = 0;
  8. unsigned long long int Output = 0;
  9. char* EncodedStr = NULL;
  10. int Size = 0;
  11. while(true) {
  12. cin >> Input;
  13. Size = Encode(Input, &EncodedStr);
  14. Output = Decode(EncodedStr, Size);
  15. cout << EncodedStr << endl;
  16. cout << Output << endl;
  17. }
  18. }
  19. 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', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ы', 'ь', 'э', 'ю', 'я', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ь', 'Ы', 'Ь', 'Э', 'Ю', 'Я'};
  20. const int WordAmount = sizeof(WordMap) / sizeof(char);
  21. int Encode(unsigned long long int Input, char** Output) {
  22. int I = 0;
  23. int K = 0;
  24. unsigned long long int D = 0;
  25. unsigned long long int Previous = Input;
  26. while(Input > pow(WordAmount, I + 1)) {
  27. I += 1;
  28. }
  29. const int Size = I + 1;
  30. *Output = new char[Size + 1];
  31. (*Output)[Size] = 0;
  32. while(I >= 0) {
  33. D = powl(WordAmount, I);
  34. K = Previous / D;
  35. (*Output)[Size - I - 1] = WordMap[K];
  36. Previous -= D * K;
  37. I -= 1;
  38. }
  39. return Size;
  40. }
  41. unsigned long long int Decode(const char* Input, int Size) {
  42. int I = Size - 1;
  43. int K = 0;
  44. unsigned long long int Previous = 0;
  45. while(I >= 0) {
  46. for(size_t i = 0; i < WordAmount; i++) {
  47. if(WordMap[i] == Input[Size - I - 1]) {
  48. K = i;
  49. break;
  50. }
  51. }
  52. Previous += K * (unsigned long long int)powl(WordAmount, I);
  53. I -= 1;
  54. }
  55. return Previous;
  56. }

这是已修复的代码。

英文:

Fixed the code. It works now. The problem was in algorithm

  1. #include &quot;pch.h&quot;
  2. #include &lt;iostream&gt;
  3. int Encode(unsigned long long int, char**);
  4. unsigned long long int Decode(const char*, int);
  5. using namespace std;
  6. int main() {
  7. setlocale(LC_CTYPE, &quot;rus&quot;);
  8. unsigned long long int Input = 0;
  9. unsigned long long int Output = 0;
  10. char* EncodedStr = NULL;
  11. int Size = 0;
  12. while(true) {
  13. cin &gt;&gt; Input;
  14. Size = Encode(Input, &amp;EncodedStr);
  15. Output = Decode(EncodedStr, Size);
  16. cout &lt;&lt; EncodedStr &lt;&lt; endl;
  17. cout &lt;&lt; Output &lt;&lt; endl;
  18. }
  19. }
  20. 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;};
  21. const int WordAmount = sizeof(WordMap) / sizeof(char);
  22. int Encode(unsigned long long int Input, char** Output) {
  23. int I = 0;
  24. int K = 0;
  25. unsigned long long int D = 0;
  26. unsigned long long int Previous = Input;
  27. while(Input &gt; pow(WordAmount, I + 1)) {
  28. I += 1;
  29. }
  30. const int Size = I + 1;
  31. *Output = new char[Size + 1];
  32. (*Output)[Size] = 0;
  33. while(I &gt;= 0) {
  34. D = powl(WordAmount, I);
  35. K = Previous / D;
  36. (*Output)[Size - I - 1] = WordMap[K];
  37. Previous -= D * K;
  38. I -= 1;
  39. }
  40. return Size;
  41. }
  42. unsigned long long int Decode(const char* Input, int Size) {
  43. int I = Size - 1;
  44. int K = 0;
  45. unsigned long long int Previous = 0;
  46. while(I &gt;= 0) {
  47. for(size_t i = 0; i &lt; WordAmount; i++) {
  48. if(WordMap[i] == Input[Size - I - 1]) {
  49. K = i;
  50. break;
  51. }
  52. }
  53. Previous += K * (unsigned long long int)powl(WordAmount, I);
  54. I -= 1;
  55. }
  56. return Previous;
  57. }

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:

确定