大数组(常见问题6.14的混乱) [英] Big arrays (confusion over FAQ 6.14)

查看:66
本文介绍了大数组(常见问题6.14的混乱)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个大于size_t - 1的数组,其中我认为这是最大的保证数组。我虽然在FAQ 6.14中找到了

救赎,但看来我不理解

的东西。


array是指向结构的指针数组。这是一个使用小数组的示例




/ *检查malloc是否成功,main'返回等等是否已移除

简洁* /


#include< stdio.h>

#include< stdlib.h>

typedef struct {

int foo;

int bar;

} example_struct;


int main(void){

example_struct * small_array [1000];

example_struct * a_single_struct;


a_single_struct = malloc(sizeof(example_struct *));

a_single_struct-> foo = 4000;

printf(" a_single_struct foo:%i \ n",a_single_struct-> ; foo);


small_array [900] = a_single_struct;

printf(" small_array 900 foo:%i \ n",small_array [900] - > foo);


}


这很好用:


a_single_struct foo: 4000

small_array 900 foo:4000


到目前为止,非常好。现在我想做一个例如3,000,000

成员的数组,对于简单的big_array [3000000]声明来说太大了。所以我

使用来自常见问题6.14的代码:


/ *检查malloc是否成功,main'返回等等是否已移除

简洁* /


#include< stdio.h>

#include< stdlib.h>

typedef struct {

int foo;

int bar;

} example_struct;


int main(void){

example_struct * big_array;

example_struct * a_single_struct;


a_single_struct = malloc(sizeof (example_struct *));

a_single_struct-> foo = 4000;

printf(" a_single_struct foo:%i \ n",a_single_struct-> foo) ;


big_array = malloc(3000000 * sizeof(example_struct *));

big_array [1000000] = a_single_struct; / *第20行,错误低于* /

printf(" big_array foo:%i \ n",big_array [1000000] - > foo);


}


不行。编译器(在本例中为gcc)返回:


test.c:在函数''main''中:

test.c:20:错误:分配中不兼容的类型

test.c:21:错误:无效类型参数'' - >''


因此,FAQ 6.14中的示例是:


" int * dynarray;

dynarray = malloc(10 * sizeof(int));


....你可以引用dynarray [i](对于i从0到9)几乎就像

dynarray是一个传统的,静态分配的数组(int a [10])" br />

鉴于此,我很困惑为什么


small_array [900] = a_simple_struct;


有效,而


big_array [1000000] = a_simple_struct;


没有。


我的代码中是否有明显的错误?我是不是因为在comp.lang.c众神面前展示我的观点而尴尬地尴尬了?或者是

有一些更好的创建数组的方法(size_t - 1)?


谢谢。

I have a need to create an array that is larger than size_t - 1, which
I believe is the largest guaranteed array. I though I''d found
salvation in FAQ 6.14, but it appears I am not understanding
something.

The array is an array of pointers to structs. Here is an example
using a small array:

/* checks that malloc succeeded, main''s return, etc. removed for
brevity */

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

typedef struct {
int foo;
int bar;
} example_struct;

int main (void) {
example_struct * small_array[1000];
example_struct * a_single_struct;

a_single_struct = malloc ( sizeof(example_struct *) );
a_single_struct->foo = 4000;
printf ("a_single_struct foo: %i \n", a_single_struct->foo);

small_array[900] = a_single_struct;
printf ("small_array 900 foo: %i\n",small_array[900]->foo);

}

This works fine:

a_single_struct foo: 4000
small_array 900 foo: 4000

So far, so good. Now I want to do an array of, say, 3,000,000
members, too large for a simple big_array[3000000] declaration. So I
use the code from faq 6.14:

/* checks that malloc succeeded, main''s return, etc. removed for
brevity */

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

typedef struct {
int foo;
int bar;
} example_struct;

int main (void) {
example_struct * big_array;
example_struct * a_single_struct;

a_single_struct = malloc ( sizeof(example_struct *) );
a_single_struct->foo = 4000;
printf ("a_single_struct foo: %i \n", a_single_struct->foo);

big_array = malloc ( 3000000 * sizeof ( example_struct * ) );
big_array[1000000] = a_single_struct; /* line 20, error below */
printf ("big_array foo: %i\n",big_array[1000000]->foo);

}

No go. The compiler (gcc in this case) returns:

test.c: In function ''main'':
test.c:20: error: incompatible types in assignment
test.c:21: error: invalid type argument of ''->''

So the example in FAQ 6.14 is:

"int *dynarray;
dynarray = malloc(10 * sizeof(int));

....you can reference dynarray[i] (for i from 0 to 9) almost as if
dynarray were a conventional, statically-allocated array (int a[10])"

Given that, I''m confused why

small_array[900] = a_simple_struct;

works, while

big_array[1000000] = a_simple_struct;

doesn''t.

Is there some obvious error in my code? Have I just embarrassed
myself by displaying my obtusity before the comp.lang.c gods? Or is
there some better method of creating arrays (size_t - 1) ?

Thanks.

推荐答案

Andrew Fabbro写道:
Andrew Fabbro wrote:

>

我需要创建一个数组这大于size_t - 1,

我认为是最大的保证阵列。我虽然已经在FAQ 6.14中找到了救赎,但看起来我并没有理解某些东西。
>
I have a need to create an array that is larger than size_t - 1,
which I believe is the largest guaranteed array. I though I''d
found salvation in FAQ 6.14, but it appears I am not
understanding something.



你不能拥有它。没有任何物体的大小无法用

size_t来衡量。你想做什么?


-

Chuck F(cinefalconer at maineline dot net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>


-

已发布通过 http://www.teranews.com 上的免费Usenet帐户

You can''t have it. No object whose size cannot be measured in a
size_t can exist. What are you trying to do?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com




" CBFalconer" < cb ******** @ yahoo.comwrote in message

news:46 *************** @ yahoo.com ...

"CBFalconer" <cb********@yahoo.comwrote in message
news:46***************@yahoo.com...

Andrew Fabbro写道:
Andrew Fabbro wrote:

>>
我需要创建一个大于size_t - 1的数组,我认为这是最大的保证数组。我虽然在FAQ 6.14中找到了救赎,但似乎我并没有理解某些东西。
>>
I have a need to create an array that is larger than size_t - 1,
which I believe is the largest guaranteed array. I though I''d
found salvation in FAQ 6.14, but it appears I am not
understanding something.



你不能拥有它。没有任何物体的大小无法用

size_t来衡量。你想做什么?


You can''t have it. No object whose size cannot be measured in a
size_t can exist. What are you trying to do?



它可以存在于后备商店。

只要你不需要太多随机访问你的数组,将它存储在磁盘上并按需要读取块。

不幸的是fseek()函数经常需要很长时间,可能不会

够大了。 fsetpos()接受另一种数据类型fpos_t,并且可能

不可用。


-

免费游戏和编程好东西。
http://www.personal.leeds .ac.uk / ~bgy1mm

It can exist in the backing store.
As long as you don''t need too many random accesses to your array, store it
on disk and read in chunks as you need.
Unfortunately the fseek() function quite often takes a long, which might not
be big enough. The fsetpos() takes yet another data type, fpos_t, and may
not be available.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


2007年8月11日星期六22:09:57 -0700,Andrew Fabbro写道:
On Sat, 11 Aug 2007 22:09:57 -0700, Andrew Fabbro wrote:

我需要创建一个大于size_t - 1的数组,其中我认为这是最大的保证数组。我虽然在FAQ 6.14中找到了

救赎,但看来我不理解


I have a need to create an array that is larger than size_t - 1, which
I believe is the largest guaranteed array. I though I''d found
salvation in FAQ 6.14, but it appears I am not understanding
something.



在现代PC上,(size_t)( - 1)小于4 GB(在32位

来访问它。你到底想要做什么?您可以将所有这些结构存储在磁盘文件中。

(如果您正在为MS-DOS或其他系统编程,那么它是

比(size_t)( - 1)有更多的内存是荒谬的,转到

comp.os.msdos.programming或其他什么。)

-

Army1987(将NOSPAM替换为电子邮件)

没有人通过辞职来赢得比赛。 - S. Tartakower

On modern PCs, (size_t)(-1) is one byte less than 4 GB (on 32-bit
processors), or about 2.8 GB times the human population of the
Earth (on 64-bit processors). You are very likely *not* to have
enough memory, and if you have I think is impossible (or almost so)
to access it. What the hell are you trying to do? You might store
all those structs on a disk file.
(If you''re programming for MS-DOS or some other system where it is
not absurd to have more memory than (size_t)(-1), go to
comp.os.msdos.programming or whatever.)
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower


这篇关于大数组(常见问题6.14的混乱)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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