使用stackoverflow错误初始化数组 [英] initialize array with stackoverflow error

查看:75
本文介绍了使用stackoverflow错误初始化数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的方法:

void loadContent()
{
    int test[500000];
    test[0] = 1;
}

当我调用此方法时,会发生此错误:

when I call this method, this error occurs:

RasterizeEditor.exe中0x00007FF639E80458的未处理异常:0xC00000FD:堆栈溢出(参数:0x0000000000000001,0x000000AE11EB3000).

Unhandled exception at 0x00007FF639E80458 in RasterizeEditor.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000AE11EB3000).

我将arraysize更改为100,并且一切正常……但是我需要500000整数值,那我该怎么办?C ++必须能够在一个数组中管理500000个int值,问题可能出在其他地方吗?还是只需要更改Visual Studio 2015的设置?

I changed the arraysize to 100 and everything is working fine... but I need 500000 integer values, so what should I do? C++ must be able to manage 500000 int values in one array, can the problem be somewhere else? or does I just have to change a setting of visual studio 2015?

推荐答案

当您创建这样的数组时,它位于堆栈"中,但是显然堆栈的大小是不够的.

When you create array like this, it's located into the "stack", but apparently your stack'size isn't sufficient.

您可以尝试将其放置在另一个位置,例如堆"(具有malloc之类的动态内存分配),例如:

You can try to place it into another place, like the "heap" (with dynamic memory allocation like malloc) like:

#include <stdlib.h>
#include <stdio.h>

void loadContent()
{
  int    *test = malloc(sizeof(int) * 500000);

  if (!test) {
     fprintf(stderr, "malloc error\n");
     exit(1);
  }
  test[0] = 1;
}

这篇关于使用stackoverflow错误初始化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆