小编moo*_*oha的帖子

数组变量周围的堆栈损坏

我正在尝试通过我的assign和draw函数传递数组。仅存在分配函数以获取wins数组并为该数组中的所有元素分配0值。我的绘制函数使用数组中的20个数字用随机数1-100填充数组。当我尝试编译时,我最终遇到运行时错误,指出我的变量(数组)wins周围的堆栈已损坏。我应该从这里去哪里?

#include<iostream>
#include <ctime>

using std::cout; using std::cin; using std::endl;

void draw(int a[]);
void assign(int a[]);
int sizeofarray = 20;


int main() {
    int wins[20] = {};

    assign(wins);

    cout << "compiled!" << endl;

}

void assign(int a[20]) {

    a[20] = {};
    draw(a);

}

void  draw(int a[])

{
    srand(time(nullptr));
    int rannum = (1 + rand() % 100);


    for (int i = 0; i < 20; i++) {

        a[i] = 1 + rand() % 100;
    }


}
Run Code Online (Sandbox Code Playgroud)

c++ arrays loops

1
推荐指数
1
解决办法
56
查看次数

标签 统计

arrays ×1

c++ ×1

loops ×1