是否有可能存在不会崩溃程序的内存问题

Is it possible to have memory problems that don’t crash a program?

本文关键字:程序 内存 问题 崩溃 有可能 存在 是否      更新时间:2023-10-16

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

下面我隔离了执行该文本的代码。如果它很有用,我正在运行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;
}

例如,使用包含的数组" mnbvcxzasdfghjklpoiuytrewq" 作为较低案例字母的密码,我会得到" mnbv" ,如果输入是" ABCD",则。这是对的。

如果输入为"一个长词" ,我得到" M GGGZ ZZZV" 作为输出,当它应该为" M GKJZ RKOV" <</strong>。有点正确,但仍然错误。如果我使用"这是一个非常长的句子,将导致程序失败" 作为输入,我将" uas" 作为输出,这是完全错误的。>

对于您的特定代码,您应该通过内存检查工具(例如Valgrind(运行它,或使用地址消毒器编译。

这是一些记忆问题的示例很可能不会崩溃您的程序:

  1. 忘记删除一个小物体,该对象在程序中仅分配一次。如果记忆泄漏几十年来一直未被发现,如果该程序没有使程序用完。
  2. 从分配的非初始化记忆中读取。如果系统在第一次写入时分配对象,可能仍会崩溃。
  3. 在堆在堆上的对象之后,大小为 sizeof(obj) % 8 != 0之后,略微从范围中写出。因此,由于堆分配通常是在8或16的倍数中完成的
  4. 删除nullptr不会在某些系统上崩溃。例如,AIX用于将零放置在地址0x0和附近。较新的AIX可能仍然会这样做。

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

    在我尝试过的任何系统上(基于POSIX(,可以通过内存映射在地址零上分配有效的内存。这样做甚至可以使nullptr的写作不崩溃。

这只是部分列表。

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

例如,大多数编译器都会优化:

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

进入这个:

int a = *p;
boom(p);

如果系统允许使用nullptr,则此代码在优化后可能会崩溃。它不会因删除而崩溃,但是由于优化做了一些程序员所预见的事情。