首页 > 解决方案 > file.exe 在 Visual Studio C++ 中触发了一个断点

问题描述

抱歉,这一定是一个非常基本的错误,但我到处搜索,似乎无法解决我的问题,我只是为我的 OOP 类做一些“输入和输出数组”,我对我的数组使用动态允许,这是我的代码,我知道这只是删除错误,其他(创建,插入,...工作正常),非常感谢所有人:

delete [] a; delete a也试过了,但没用,我试着把它放在system("pause")后面(我用谷歌搜索了这个),但也没有:((


void shift_left_array(int *a,int k , int &n, int step = 1) {
    // k : begin position
    // n : end position
    for (int i = k; i < n; i++) {
        *(a + i - 1) = *(a + i); // Shift right array
    }
}

void delete_from_array(int *a, int &n) {
    int k; // delete position
    // shift left, then resize array
    cout << "Which position do you want to delete ? (Type from 0 to " << n - 1 << ") : " << endl;
    cin >> k;

    shift_left_array(a, k + 1, n);
    n--; // resize array

    delete (a + n); // Trigger break point error here
}

int main() {

    int n = 0;
    int *a = new int[n];

    cout << "Insert array size : " << endl;
    cin >> n;

    // Create new array
    create_array(a,n);
    display_array(a,n);
    // Add more value to array (increase array size)
    insert_to_array(a, n);
    display_array(a, n);

    // Delete value from array
    delete_from_array(a,n);
    display_array(a, n);

    // Sort
    bubble_sort(a,n);
    display_array(a, n);
    // Search
    linear_search_array(a, n);
    // Update
    update_array_value(a, n);
    display_array(a, n);

    system("pause");
    return 0;
}

标签: c++arraysvisual-studiooopvisual-c++

解决方案


推荐阅读