程序在作用域结束后崩溃



我对以下代码感到困惑,它正在造成我完全不理解的崩溃。

#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
struct Store
{
    int pos;
    char character;
};
vector<struct Store> v_store;
void swap_inside_vector(vector<struct Store> &v_store, int a, int b)
{
    struct Store tmp = v_store[b];
    v_store[b] = v_store[a];
    v_store[a] = tmp;
}
int find_inside_vector(vector<struct Store> &v_store, int pos)
{
    for(int i = 0; i < v_store.size(); i++)
    {
        if(v_store[i].pos == pos)
            return i;
    }
    return -1;
}
void swap_most_right_to_most_left(vector<struct Store> &v_store)
{
    struct Store tmp = v_store[v_store.size() - 1];
    for(int i = v_store.size(); i > 0; i--)
    {
        v_store[i] = v_store[i-1];
    }
    v_store[0] = tmp;
}
void print_char_inside_vector(vector<struct Store> &v) // used for debugging
{
    for(int i = 0; i < v.size(); i++)
    {
        printf("%C", v[i].character);
    }
}
int check_vector_original_state(vector<struct Store> &v_store)
{
    for(int i = 1; i <= v_store.size(); i++)
    {
        if(v_store[i-1].pos != i)
            return 0;
    }
    return 1;
}
int main()
{
    int n;
    char input_str[100];
    for(int q = 1; scanf("n%d", &n), n; q++)
    {
        scanf("%s", input_str); 
        vector<struct Store> original_store, tmp_origin_store, v_store;
        vector<vector<struct Store> > store_vector;
        original_store.clear();
        tmp_origin_store.clear();
        v_store.clear();
        store_vector.clear();
        for(int i = 1; i <= strlen(input_str); i++)
        {
            struct Store s = {.pos = i, .character = input_str[i-1]};
            original_store.push_back(s);
        }
        tmp_origin_store = original_store;
        v_store = tmp_origin_store;
        for(int i = 1, current_pos = 0; i <= n; i++, current_pos++)
        {
            int vector_index = find_inside_vector(v_store, tmp_origin_store[current_pos].pos);
            //printf("Processing -> [%d], Current Pos -> [%d]n", i, current_pos);
            for(int z = 0; z < (current_pos + 1); z++)
            {
                if(vector_index == (v_store.size() - 1))
                {
                    swap_most_right_to_most_left(v_store);
                    vector_index = 0;
                }
                else
                {
                    swap_inside_vector(v_store, vector_index, vector_index + 1);
                    vector_index++;
                }
                //print_char_inside_vector(v_store);
                //puts("");
            }
            //puts("");
            store_vector.push_back(v_store);
            if(check_vector_original_state(v_store))
            {
                store_vector.push_back(v_store);
                break;
            }
            if(current_pos == (v_store.size() - 1))
            {
                tmp_origin_store = v_store;
                current_pos = -1;
            }
        }
        // debugging
        /*for(int x = 0; x < store_vector.size(); x++)
        {
            printf("[%d] -> ", x + 1);
            print_char_inside_vector(store_vector[x]);
            puts("");
        }*/
        int target_pos;
        if(store_vector.size() < n)
        {
            target_pos = n % store_vector.size();
        }
        else
        {
            target_pos = store_vector.size();
        }
        if(target_pos == 0)
        {
            printf("%d. ", q);
            print_char_inside_vector(store_vector[0]);
            puts("");
        }
        else
        {
            printf("%d. ", q);
            print_char_inside_vector(store_vector[target_pos - 1]);
            puts("");
        }
    }
}

这个程序所做的是接受来自STDIN的输入,处理它,并在STDOUT上输出它。

我期望的输入是

3 ABCD
13 ACM
3 DAEQD
4 FAEQS

预期输出为

1. CABD
2. CAM
3. DAQDE
4. FQASE

我的问题是,在STDIN中输入第四个输入后,在显示输出后,发生了崩溃。

C:Study>h.exe
3 ABCD
1. CABD
13 ACM
2. CAM
3 DAEQD
3. DAQDE
4 FAEQS
4. FQASE
      0 [main] h 9092 cygwin_exception::open_stackdumpfile: Dumping stack trace to h.exe.stackdump

根据我的观察,我认为问题出在矢量上,但这只是一个猜测。

您的代码没有编译:

//struct Store s = { .pos = i, .character = input_str[i - 1] }; // syntax not recongnized 
Store s = { i, input_str[i - 1] };   // replaced with this.  

修复此问题后,调试立即确定了向量上的越界问题,这可能导致内存损坏问题。它在swap_most_right_to_most_left():中

for (int i = v_store.size(); i > 0; i--)  {  // you start with i=v_store.size() 
    v_store[i] = v_store[i - 1];             // and you immediately get out of bounds !  
}

如果您将此说明更正为:

for (int i = v_store.size()-1; i > 0; i--) {
    v_store[i] = v_store[i - 1];
}

您将获得给定输入的预期输出。

最新更新