C++ 是否有可能出现与’;不要破坏一个程序?

C++ 是否有可能出现与’;不要破坏一个程序?,c++,C++,我写了一个文本密码程序。它似乎适用于只有几个字符长的文本字符串,但不适用于更长的字符串。它通过读取文本文件来获取输入文本。在较长的文本字符串上,它仍然可以运行而不会崩溃,但它似乎无法正常工作 下面我隔离了执行文本置乱的代码。如果它有用,我将在运行Ubuntu 19.04的虚拟机上运行它。运行代码时,在提示时输入auto。我删除了其余的代码,所以不会太长 #include <iostream> #include <string> #include <sstream&g

我写了一个文本密码程序。它似乎适用于只有几个字符长的文本字符串,但不适用于更长的字符串。它通过读取文本文件来获取输入文本。在较长的文本字符串上,它仍然可以运行而不会崩溃,但它似乎无法正常工作

下面我隔离了执行文本置乱的代码。如果它有用,我将在运行Ubuntu 19.04的虚拟机上运行它。运行代码时,在提示时输入auto。我删除了其余的代码,所以不会太长

#include <iostream>
#include <string>
#include <sstream>
#include <random>
#include <cmath>
#include <cctype>
#include <chrono>
#include <fstream>
#include <new>


bool run_cypher(char (&a)[27],char (&b)[27],char (&c)[11],char (&aa)[27],char (&bb)[27],char (&cc)[11]) {
//lowercase cypher, uppercase cypher, number cypher, lowercase original sequence, uppercase original sequence, number original sequence
std::ifstream out_buffer("text.txt",std::ios::in);
std::ofstream file_buffer("text_out.txt",std::ios::out);
//out_buffer.open();
out_buffer.seekg(0,out_buffer.end);
std::cout << "size of text: " << out_buffer.tellg() << std::endl;//debug
const int size = out_buffer.tellg();
std::cout << "size: " << size << std::endl;//debug
out_buffer.seekg(0,out_buffer.beg);
char *out_array = new char[size + 1];
std::cout << "size of out array: " << sizeof(out_array) << std::endl;//debug
    for (int u = 0;u <= size;u = u + 1) {
    out_array[u] = 0;
    }
out_buffer.read(out_array,size);
out_buffer.close();
char original[size + 1];//debug
    for (int bn = 0;bn <= size;bn = bn + 1) {//debug
    original[bn] = out_array[bn];//debug
    }//debug
    for (int y = 0;y <= size - 1;y = y + 1) {
    std::cout << "- - - - - - - -" << std::endl;
    std::cout << "out_array[" << y << "]: " << out_array[y] << std::endl;//debug
    int match;
    int case_n; //0 = lowercase, 1 = uppercase
        if (isalpha(out_array[y])) {
            if (islower(out_array[y])) {
            //std::cout << "out_array[" << y << "]: " << out_array[y] << std::endl;//debug
            //int match;
                for (int ab = 0;ab <= size - 1;ab = ab + 1) {
                    if (out_array[y] == aa[ab]) {
                    match = ab;
                    case_n = 0;
                    std::cout << "matched letter: " << aa[match] << std::endl;//debug
                    std::cout << "letter index: " << match << std::endl;//debug
                    std::cout << "case_n: " << case_n << std::endl;//debug
                    }
                }
            }
            if (isupper(out_array[y])) {
                for (int cv = 0;cv <= size - 1;cv = cv + 1) {
                    if (out_array[y] == bb[cv]) {
                    case_n = 1;
                    match = cv;
                    std::cout << "matched letter: " << bb[match] << std::endl;//debug
                    std::cout << "letter index: " << match << std::endl;//debug
                    std::cout << "case_n: " << case_n << std::endl;//debug
                    }
                }
            }
            if (case_n == 0) {
            out_array[y] = a[match];
            std::cout << "replacement letter: " << a[match] << " | new character: " << out_array[y] << std::endl;//debug
            }
            if (case_n == 1) {
            std::cout << "replacement letter: " << b[match] << " | new character: " << out_array[y] << std::endl;//debug
            out_array[y] = b[match];
            }

        }
        if (isdigit(out_array[y])) {
            for (int o = 0;o <= size - 1;o = o + 1) {
                if (out_array[y] == cc[o]) {
                match = o;
                std::cout << "matched letter: " << cc[match] << std::endl;//debug
                std::cout << "letter index: " << match << std::endl;//debug
                }
            }
        out_array[y] = c[match];
        std::cout << "replacement number: " << c[match] << " | new character: " << out_array[y] << std::endl;//debug
        }
    std::cout << "- - - - - - - -" << std::endl;
    }
std::cout << "original text: " << "\n" << original << "\n" << std::endl;    
std::cout << "encrypted text: " << "\n" << out_array << std::endl;
delete[] out_array;
return 0;
}

int main() {
const int alpha_size = 27;
const int num_size = 11;
char l_a_set[] = "abcdefghijklmnopqrstuvwxyz";
char cap_a_set[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char n_a_set[] = "0123456789";
std::cout << "sizeof alpha_set: " << std::endl;//debug
char lower[alpha_size] = "mnbvcxzasdfghjklpoiuytrewq";
char upper[alpha_size] = "POIUYTREWQASDFGHJKLMNBVCXZ";
char num[num_size] = "9876543210"; 
int p_run; //control variable. 1 == running, 0 == not running
int b[alpha_size]; //array with values expressed as index numbers
std::string mode;
int m_set = 1;
    while (m_set == 1) {
    std::cout << "Enter 'auto' for automatic cypher generation." << std::endl;
    std::cout << "Enter 'manual' to manually enter in a cypher. " << std::endl;
    std::cin >> mode;
    std::cin.ignore(1);
    std::cin.clear();
        if (mode == "auto") {
        p_run = 2;
        m_set = 0;
        }
        if (mode == "manual") {
        p_run = 3;
        m_set = 0;
        }
    }
    if (p_run == 2) { //automatic mode
    std::cout <<"lower cypher: " << lower << "\n" << "upper cypher: " << upper << "\n" << "number cypher: " << num << std::endl;//debug
    run_cypher(lower,upper,num,l_a_set,cap_a_set,n_a_set);
    return 0;//debug
    }
    while (p_run == 3) {//manual mode
    return 0;//debug    
    }
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
bool run_-cypher(char&a)[27],char&b[27],char&c[11],char&aa[27],char&bb[27],char&cc[11]){
//小写密码、大写密码、数字密码、小写原始序列、大写原始序列、数字原始序列
std::ifstream out_缓冲区(“text.txt”,std::ios::in);
流文件缓冲区的std::of(“text_out.txt”,std::ios::out);
//out_buffer.open();
out\u buffer.seekg(0,out\u buffer.end);

std::cout对于您的特定代码,您应该通过valgrind之类的内存检查工具运行它,或者使用

下面是一些内存问题的示例,它们很可能不会使程序崩溃:

  • 忘记删除一个小对象,这个对象在程序中只分配了一次。如果内存泄漏不会导致程序内存不足,那么它可能在几十年内都不会被检测到
  • 从已分配的未初始化内存读取。如果系统在第一次写入时延迟分配对象,则仍可能崩溃
  • 在位于堆上的对象(其大小为
    sizeof(obj)%8!=0
    )之后稍微写越界。这是因为堆分配通常是以8或16的倍数进行的。您可以了解它
  • 取消对
    nullptr
    的引用在某些系统上不会崩溃。例如,AIX用于在地址0x0处及其附近放置零。较新的AIX可能仍然会这样做

    在许多没有内存管理的系统上,地址0要么是常规内存地址,要么是内存映射寄存器。可以访问该内存而不会崩溃

    在我尝试过的任何系统(基于POSIX)上,都可以通过内存映射在地址0处分配有效内存。这样做甚至可以使通过
    nullptr
    进行写入工作而不会崩溃

  • 这只是部分列表

    注意:这些内存问题是未定义的行为。这意味着即使程序在调试模式下没有崩溃,编译器也可能在优化过程中假设错误。如果编译器假设错误,它可能会创建优化后崩溃的优化代码

    例如,大多数编译器都会对此进行优化:

    int a = *p; // implies that p != nullptr
    if (p) 
       boom(p);
    
    为此:

    int a = *p;
    boom(p);
    

    如果系统允许取消引用
    nullptr
    ,则此代码可能在优化后崩溃。它不会因为取消引用而崩溃,而是因为优化做了程序员没有预见到的事情。

    是否可能存在不会使程序崩溃的内存问题?是的,您可能损坏了堆或其他UB wi如果不使程序崩溃。内存问题通常是未定义的行为。即使你没有使程序崩溃,你也应该尽力避免。未定义的行为并不意味着程序会崩溃。有时,如果你非常不幸,当程序仍然格式错误且行为未定义时,你会得到预期的结果。非常非常rief浏览此代码可以发现导致未定义行为的几种可能的执行路径,其中使用了未初始化的变量。Boom。使用调试器一次执行一行程序,并观察其逻辑执行流。这就是调试器的用途。
    char*out\u array=new char[size+1]我不能谢谢你。我运行了valgrind,结果发现这不是内存问题。问题是一个我没有注意到的无限循环。