帮助:如何创建一个包含10000000个元素的巨大数组? [英] Help:How can I create a Huge array with 10000000 elements?

查看:164
本文介绍了帮助:如何创建一个包含10000000个元素的巨大数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器:Visual C ++ 6.0

我的代码:

// ===================== =========

#include" stdio.h"

#include" stdlib.h"


int main(){

int v [10000000];

long i;

for(i = 1; i< = 10000000; i ++)v [i] = i;

返回0;

}

// ========= =====================

编译或链接时没有任何错误或警告。但是当我在可执行文件中运行

时,会出现错误。为什么?


感谢您的帮助。

Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"

int main() {
int v[10000000];
long i;
for (i=1; i<=10000000; i++) v [ i ]=i;
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?

Thanks for your help.

推荐答案



zehanw ... @ gmail.com写道:

zehanw...@gmail.com wrote:
编译器:Visual C ++ 6.0
我的代码:
// ============ ==================
#include" stdio.h"
#include" stdlib.h"

int main(){
int v [10000000];
long i;
for(i = 1; i< = 10000000; i ++)v [i] = i;
返回0;
}
// ==============================

编译或链接时没有任何错误或警告。但是当我运行可执行文件时,会出现错误。为什么?


因为数组索引从0开始?

感谢您的帮助。
Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"

int main() {
int v[10000000];
long i;
for (i=1; i<=10000000; i++) v [ i ]=i;
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?
Because arrays index from 0?

Thanks for your help.





ze*******@gmail.com 写道:
编译器:Visual C ++ 6.0
我的代码:
// ============================== br /> #include" stdio.h"
#include" stdlib.h"

如果要包含系统标题,请使用尖括号:

#include< stdio.h>

#include< stdlib.h>

int main(){
int v [10000000 ];
我很久;


如果你正在索引一个数组,size_t是一个比long更合适的类型,

虽然没有错。 (嘿,押韵。)

for(i = 1; i< = 10000000; i ++)v [i] = i;


您的程序访问v [10000000],这不是数组的一部分。它只需要v [9999999],因为数组从0开始。写下这样的

循环:


for(i = 0; i< 10000000; i ++)v [i] = i;


或者更好(如果你没有,请查看详细信息)理解它):


for(i = 0; i< sizeof v / sizeof v [0]; i ++)v [i] = i;


最后,请注意,您尝试分配给数组的值

元素超过了保证适合int的最大值(32767)。

他们将适合32位平台,但保持良好状态仍然很好。如果要确保可以存储这些值,请将数组元素long的类型设为

,而不是int;然后它可以在任何平台上运行。

返回0;
}
// ==================== ==========

编译或链接时没有任何错误或警告。但是当我运行可执行文件时,会出现错误。为什么?
Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"
If you want to include system headers, use angular brackets:

#include <stdio.h>
#include <stdlib.h>
int main() {
int v[10000000];
long i;
If you''re indexing an array, size_t is a more suitable type than long,
although it''s not wrong. (Hey, that rhymes.)
for (i=1; i<=10000000; i++) v [ i ]=i;
Your program accesses v[10000000], which is not part of the array. It
only goes up to v[9999999], since arrays start from 0 in C. Write the
loop like this:

for (i = 0; i < 10000000; i++) v[i] = i;

Or better yet (and look up the details of this if you don''t understand it):

for (i = 0; i < sizeof v / sizeof v[0]; i++) v[i] = i;

Finally, note that the values you''re trying to assign to the array
elements exceed the maximum value guaranteed to fit in an int (32767).
They will fit on a 32-bit platform, but it is still good to keep in
mind. If you want to make sure these values can be stored, make the type
of the array elements `long'', not `int''; then it will work on any platform.
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?




除了上面的问题,你还试图使用一个40亿美元的数组(我是猜测你正在编译Win32平台,其中
的整数是4字节大。)这超过了1 MB的默认堆栈

Visual C ++为应用程序设置。


如果你*真的*希望你的程序在
启动,告诉编译器应该允许这样做。搜索

堆栈大小在文档中。如果做不到这一点,请让你的阵列更小,或者动态分配它。


S.



Aside from the problems above, you''re trying to use an array of 40
million bytes (I''m guessing you''re compiling for a Win32 platform, which
has ints that are 4 bytes large). This exceeds the default stack of 1 MB
Visual C++ sets for applications.

If you *really* want your program to allocate 40 million bytes on
startup, tell the compiler that this should be allowed. Search for
"stack size" in the documentation. Failing that, make your array
smaller, or allocate it dynamically.

S.


ze*******@gmail.com 写道:
编译器:Visual C ++ 6.0
我的代码:
// ==============================
#包括stdio.h
#include" stdlib.h"
int main(){
int v [10000000];
long i;
for(i = 1; i< = 10000000; i ++)v [i] = i;
返回0;
}
// ========= =====================

编译或链接时没有任何错误或警告。但是当我运行可执行文件时,会出现错误。为什么?
Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"

int main() {
int v[10000000];
long i;
for (i=1; i<=10000000; i++) v [ i ]=i;
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?




发生了什么错误?


有些系统对变量的大小有限制。你可能是
能够用malloc()分配更多空间而不是局部变量

声明,但是没有保证。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http ://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



What error occurred?

Some systems impose limits on how big a variable can be. You might be
able to allocate more space with malloc() than with a local variable
declaration, but there are no guarantees.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于帮助:如何创建一个包含10000000个元素的巨大数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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