简单数组导致异常 [英] simple array cause exception

查看:78
本文介绍了简单数组导致异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    float x[1000][1000];

    return 0;
}

我在s.exe中获得 0x01341637的第一次机会异常:0xC00000FD:堆栈溢出。为什么?

I get " First-chance exception at 0x01341637 in s.exe: 0xC00000FD: Stack overflow." why?

推荐答案

您的数组太大了,无法放入堆栈。您没有足够的堆栈空间来容纳 1000 * 1000 元素。

Your array is simply too large to fit on the stack. You don't have enough stack space for 1000 * 1000 elements.

您需要分配数组在堆上。您可以使用 new 关键字完成此操作,但更简单的方法是仅使用 std :: vector

You'll need to allocate your array on the heap. You can do this using the new keyword, but an easier way is to just use std::vector.

std::vector<std::vector<float> > floats(1000);
for (unsigned i = 0; i != floats.size(); ++i) floats[i].resize(1000);

这将为您提供一个二维浮点矢量,每个矢量包含1000个元素。

This will give you a two-dimensional vector of floats, with 1000 elements per vector.

另请参见:分段错误在大型数组上

这篇关于简单数组导致异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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