动态数组 [英] Dynamic Array

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

问题描述

假设你想要在数组中存储n个记录,并且数组可以保存所有n个元素,也就是说,它被声明为int [n]。


并且在运行时突然间,我需要存储n + m个项目。为什么不能这样做



a [0] = e(0)

a [1] = e(1)

...

...

a [n] = e(n)


其中e(i)是第i个元素

*(a +(n + 1))= e(n之后的第1个元素)

...

...

*(a +(n + 1))= e(n后的第m个元素)


开如果我分配一个包含0个元素a [0]的数组,C将让我

填写大约5个值。


有没有制作索引表的方法一个阵列成长?

-


谢谢


Ronny Mandal

这封电子邮件和任何附件都是保密的,可以享有特权或

,否则不予披露。它仅供上述人员使用。
。如果您不是预期的收件人,则严禁任何阅读,使用,b / b
披露,复制或分发本电子邮件的全部或部分内容或

相关附件。如果您不是预定的

收件人,请立即回复此邮件

或通过电话通知发件人并永久删除此电子邮件和任何附件

你的系统。

Assume that you want to store n records in an array, and that the array can
hold all n elements, that is, it is declared int a[n].

And suddenlny in run-time, I need to store n+m items. Why cannot this be
done:

a[0] = e(0)
a[1] = e(1)
...
...
a[n] = e(n)

where e(i) is the i-th element
*(a+ (n+1) ) = e(the 1st element after n)
...
...
*(a+ (n+1) ) = e(the m-th element after n)

On the ocntrary, if I allocate an array with 0 elements a[0], C will let me
fill in approx 5 more values.

Is there a way to make the "index-table" of an array grow?
--

Thanks

Ronny Mandal
This e-mail and any attachment are confidential and may be privileged or
otherwise protected from disclosure. It is solely intended for the person(s)
named above. If you are not the intended recipient, any reading, use,
disclosure, copying or distribution of all or parts of this e-mail or
associated attachments is strictly prohibited. If you are not an intended
recipient, please notify the sender immediately by replying to this message
or by telephone and delete this e-mail and any attachments permanently from
your system.

推荐答案

在文章< d3 ********** @ readme.uio中。 no>,

Ronny Mandal< ro ***** @ math.uio.no>写道:
In article <d3**********@readme.uio.no>,
Ronny Mandal <ro*****@math.uio.no> wrote:
假设你想在数组中存储n条记录,并且数组可以包含所有n个元素,也就是说,它被声明为int [n]。
在运行时突然间,我需要存储n + m个项目。为什么不能这样做:
*(a +(n + 1))= e(n之后的第1个元素)
Assume that you want to store n records in an array, and that the array can
hold all n elements, that is, it is declared int a[n]. And suddenlny in run-time, I need to store n+m items. Why cannot this be
done: *(a+ (n+1) ) = e(the 1st element after n)




因为什么时候你声明int a [n](假设n有

被定义为#define')然后你告诉

C保留-exactly- n存储整数的位置,以及

你告诉C,在那之后存放它需要的任何东西是好的。




如果你想要C中的*任何*动态调整大小,那么你需要使用malloc()或alloc()或者等效的
。每个malloc()

请求都会在通话中授予你请求的存储量

,如果没有未定义的话,你就不能存储。

行为。但是,malloc()允许您在运行时传入大小

而不是C所需的固定大小 - []数组的-compile-

时间。


如果你使用动态内存作为一个数组,并且你发现你需要更多位置在数组中,那么你可以使用realloc()

要求提供更多存储空间。 realloc()将

不一定扩展数组就地:相反,如果有必要,

它将创建一个具有更大尺寸的新对象,复制

旧对象的内容到新的,并返回新的地址

。哪个没关系,除非你以前曾经指点旧指针,因为那些指针

可能不再有效......


如果你需要动态数组的效果,你需要任何一个元素的位置

在你分配它之后不要改变你需要/ b $ b需要能够增长数组,然后你需要使用技术

,如链表或二叉树,或2-3树,或尝试;或者

您可以找到几个预先编写的稀疏数组中的一个实现并使用它们而不用担心
$ b下的内容b。引擎盖。

-

没有人有权通过

要求经验证据来摧毁他人的信念" - Ann Landers



Because when you declare int a[n] (presuming that n has
been #define''d with a constant value) then you are telling
C to reserve -exactly- n locations to store integers, and
you are telling C that it is fine to store whatever it needs to
in the location after that.

If you want *anything* in C to be dynamically sized, then you
must use malloc() or alloc() or the equivilent. Each malloc()
request will grant the amount of storage that you requested
in the call, and you can''t store beyond that without undefined
behaviour. However, malloc() allows you to pass in a size
at run-time rather than the fixed sizes that C requires at -compile-
time for [] arrays.

If you are using dynamic memory as an array, and you find you
need more locations in the array, then you can use realloc()
to ask that more storage be made available. realloc() will
not necessarily extend the array "in place": instead, if necessary,
it will create a new object with the larger size, copy the
contents of the old object to the new one, and return the address
of the new one. Which is fine unless you happened to have been
taking pointers to the old object before, because those pointers
might not be valid anymore...

If you need the effect of dynamic arrays and you need the location
of any one element not to change after you allocate it and you
need to be able to grow the array, then you need to use a techique
such as linked lists or binary trees, or 2-3 trees, or tries; or
you could find one of the several pre-written "sparse array"
implementations and use them without worrying about what''s under
the hood.
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers


你可能想看一下realloc()。


如果你有一个数组'' a'已经使用malloc分配,

realloc()允许你动态调整内存量

分配给''a''(即在这种情况下) ,有效地调整数组大小)。

例如,假设使用以下代码分配''a'':


int * a;


if(!(a = malloc(sizeof(int)* N))){

/ *一些内存不足错误处理代码* /

}


你可以按照''a''的相同方式操作a,如果''a''

是使用int a [N];在堆栈上分配。例如:


/ *一些随机代码使用刚刚分配的数组* /

for(i = 0; i< N; i ++){a [我] =我; }


然后,如果你以后需要调整''a''以便它有M个元素你可以

使用以下内容:


if(!(a = realloc(a,sizeof(int)* M))){

/ *一些内存不足错误处理代码* /

}


调用realloc后,您可以自由使用

范围内的索引0 ..(M-1)。


/ *使用刚刚重新分配的数组的更多随机代码* /

for(i = N; i< M; i ++){a [i ] = i; }


希望有所帮助


-Dan

Ronny Mandal写道:
You might want to have a look at realloc().

If you have an array ''a'' that was already allocated using malloc,
realloc() allows you to dynamically resize the amount of memory
allocated to ''a'' (i.e. in this case, effectively resizing the array).
For example, assume that ''a'' was allocated using the following code:

int *a;

if (!(a = malloc(sizeof(int)*N))) {
/* some out of memory error handling code */
}

You could then operate on ''a'' in the same manner you would use if ''a''
was allocated on the stack using "int a[N];". e.g.:

/* some random code using the just allocated array */
for (i = 0; i < N; i++) { a[i] = i; }

Then if you later need to resize ''a'' so that it has M elements you could
use the following:

if (!(a = realloc(a, sizeof(int)*M))) {
/* some out of memory error handling code */
}

After calling realloc, you would then be free to use indices in the
range of 0..(M-1).

/* some more random code using the just reallocated array*/
for (i = N; i < M; i++) { a[i] = i; }

Hope that helps

-Dan
Ronny Mandal wrote:
假设你想要在数组中存储n个记录,并且数组可以包含所有n个元素,也就是说,它被声明为int [n]。

并且在运行中突然出现 - 时间,我需要存储n + m个项目。为什么不能这样做:

a [0] = e(0)
a [1] = e(1)
...
...
a [n] = e(n)

其中e(i)是第i个元素

*(a +(n + 1))= e(n后的第1个元素)
......
*(a +(n + 1))= e(n后的第m个元素)

在ocntrary上,如果我分配一个包含0个元素a [0]的数组,C将让我
填写大约5个值。

有没有一种制作索引表的方法。一个数组的增长?
Assume that you want to store n records in an array, and that the array can
hold all n elements, that is, it is declared int a[n].

And suddenlny in run-time, I need to store n+m items. Why cannot this be
done:

a[0] = e(0)
a[1] = e(1)
...
...
a[n] = e(n)

where e(i) is the i-th element
*(a+ (n+1) ) = e(the 1st element after n)
...
...
*(a+ (n+1) ) = e(the m-th element after n)

On the ocntrary, if I allocate an array with 0 elements a[0], C will let me
fill in approx 5 more values.

Is there a way to make the "index-table" of an array grow?



On Sun,2005年4月17日19:22:02 +0200,Ronny Mandal
< ro ***** @ math.uio.no>在comp.lang.c中写道:
On Sun, 17 Apr 2005 19:22:02 +0200, "Ronny Mandal"
<ro*****@math.uio.no> wrote in comp.lang.c:
假设你想在数组中存储n条记录,并且数组可以包含所有n个元素,即它声明为int [n]。

在运行时突然间,我需要存储n + m个项目。为什么不能这样做:

a [0] = e(0)
a [1] = e(1)
..
..
a [n] = e(n)

其中e(i)是第i个元素

*(a +(n + 1) )= e(n后的第1个元素)
..
..
*(a +(n + 1))= e(n后的第m个元素)

在ocntrary上,如果我分配一个包含0个元素a [0]的数组,C将让我
Assume that you want to store n records in an array, and that the array can
hold all n elements, that is, it is declared int a[n].

And suddenlny in run-time, I need to store n+m items. Why cannot this be
done:

a[0] = e(0)
a[1] = e(1)
..
..
a[n] = e(n)

where e(i) is the i-th element
*(a+ (n+1) ) = e(the 1st element after n)
..
..
*(a+ (n+1) ) = e(the m-th element after n)

On the ocntrary, if I allocate an array with 0 elements a[0], C will let me




如果编译器编译定义数组的程序[0]元素,

它不是C编译器。至少不是你调用它的方式。

你可能认为它正在编译C,但事实上它正在编译一个类似C的

语言,它有自己的非标准扩展。


如果要定义的数组大小为
小于1个元素,则违反约束,并且真正的C编译器必须发出诊断为

这个。


-

杰克克莱恩

主页: http://JK-Technology.Com

常见问题

comp.lang.c http:// www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang。 learn.c-c ++
http://www.co ntrib.andrew.cmu.edu/~a...FAQ-acllc.html



If your compiler compiles a program defining an array of [0] elements,
it is not a C compiler. At least not the way you are invoking it.
You may think it is compiling C, but in fact it is compiling a C-like
language with its own non-standard extensions.

It is a constraint violation for an array to be defined with a size of
less than 1 element, and a real C compiler must issue a diagnostic for
this.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


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

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