【问题标题】:Assigning chars from static char array to dynamically allocated char array - Access violation将静态字符数组中的字符分配给动态分配的字符数组 - 访问冲突
【发布时间】:2020-04-22 13:28:57
【问题描述】:

我是 C++ 新手,玩得很开心。而且我不明白为什么这段代码不起作用并抛出访问内存冲突异常。 它可以工作,但是当我 6 岁时它会抛出异常

我在这里所做的是尝试使用 128 位数字系统。

代码 -

#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;
}

【问题讨论】:

  • 为什么不简单地使用std::string 而不是char *?另外,如果指数是整数,不要使用pow,尤其是对于这样的程序。 pow是一个浮点函数,你的程序中不需要引入浮点(另外还有舍入错误的风险)。
  • 我真的不想使用 std::string,我觉得这对我来说太过分了。我也不知道 std::string 的大小。对于这个程序,情况并非如此,但无论如何
  • 我真的不想使用std::string,我觉得这对我来说太过分了 -- 你认为为什么会这样?由于使用new char [ ],您的代码当前存在内存泄漏。还将char* Encoded = new char[Size];std::string Encoded(Size, 0); 进行比较——没有内存泄漏。
  • 是的,那我就用它。但无论如何,这让我很好奇如何以这种方式解决它。
  • Encoded[Size - I - 1] = WordMap[Previous / D] -- 您应该使用调试器来检查这些值。 WordMap 索引超出范围。请阅读how to debug small programs

标签: c++ arrays pointers exception char


【解决方案1】:

修复了代码。现在可以了。问题出在算法上

#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;
}

【讨论】:

    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2014-01-04
    • 1970-01-01
    相关资源
    最近更新 更多