Dynamic Arrays / Static Arrays

If I call SetLength on a dynamic array, does it become more or less a static array?

In use is there much difference between a dynamic and a static array?

The reason I ask is becaue I don't seem to have had much use for dynamic arrays in what I've created in the past but due to a change of approach in one program I now need to declare an array without knowing how many elements it will have. In the previous incarnation I always knew in advance that the array would have 15 elements.

I don't know why I've not had to ask this question before, but as usual, when I do have to ask it I could do with a quick answer!

Diolch,

Sion.
0
Sion
6/23/2011 10:21:49 PM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

7 Replies
941 Views

Similar Articles

[PageSpeed] 31

> {quote:title=Sion Jones wrote:}{quote}
> If I call SetLength on a dynamic array, does it become more or less a static array?
> 
> In use is there much difference between a dynamic and a static array?
> 
> The reason I ask is becaue I don't seem to have had much use for dynamic arrays in what I've created in the past but due to a change of approach in one program I now need to declare an array without knowing how many elements it will have. In the previous incarnation I always knew in advance that the array would have 15 elements.
> 
> I don't know why I've not had to ask this question before, but as usual, when I do have to ask it I could do with a quick answer!
> 

If you mean can you access items of a dynamic array like a[i] the answer is yes.

If you have to loop through dynamic array you can use for i := low(a) to high(a) do 

I hope this quick answer will help you :)

Dalija Prasnikar
0
Dalija
6/23/2011 10:34:27 PM
Sion Jones wrote:

> If I call SetLength on a dynamic array, does it become more or less a
> static array?
> 
> In use is there much difference between a dynamic and a static array?
> 
> The reason I ask is becaue I don't seem to have had much use for
> dynamic arrays in what I've created in the past but due to a change
> of approach in one program I now need to declare an array without
> knowing how many elements it will have. In the previous incarnation I
> always knew in advance that the array would have 15 elements.
> 
> I don't know why I've not had to ask this question before, but as
> usual, when I do have to ask it I could do with a quick answer!
> 
> Diolch,
> 
> Sion.

A static array has a length that is set by the system and can't be
changed without a re-compile. A dynamic array has no length until
SetLength is called on it. The length of a dynamic array can be made
shorter or longer in code at any time. Doing so preserves existing
content as much as possible.

HTH, Glynn

-- 
"God is love, but get it in writing." -- Gypsy Rose Lee.
0
Glynn
6/23/2011 10:43:37 PM
Sion Jones wrote:

> If I call SetLength on a dynamic array, does it become more or less a
> static array?

No, although the difference may not matter, depending on your use of
the array.

A static array is a value type, its size is known at compile time, the
compiler allocates memory for it in an appropriate place (where depends
on where you declare the array variable) and since it knows the
resulting address it can code access to the array items based on that
address. The array variable directly holds the array data (the address
of the variable is the address of the first item in the array).

A dynamic array is a reference type. The array variable holds only a
pointer (its size is Sizeof(pointer), 4 bytes at the moment). The
pointer is nil until you call SetLength. This function allocates a
memory block (or several for an array with more than one dimension)
from the heap and stores this block's address into the array variable.
Since this address is not known at compile-time the compiler has to
code access to the array items with an indirect memory reference (a
different assembler instruction than the one used for static arrays).
> 
> In use is there much difference between a dynamic and a static array?

Well, in general, no. There are some cases where it may be important to
know how these types are implemented, though.

 If you define a local array variable inside a method, for example, it
is useful to know that the memory for the array, if it is a static
array type, will come out of the stack, while the memory for a dynamic
array always comes from the heap. Stack memory is limited to 1 MByte by
default per thread, so you can run into problems if you use large
static array types for local variables, especially in recursive methods.

 There is also a difference in how arrays are passed as parameters to a
method: if you pass an array without the const, var, or out keyword (by
value) the behaviour is quite different for the two array types. If you
change an item of such an array inside the method the compiler will add
code for a static array that makes a copy of the passed array. You only
modify the copy, the original array you passed as parameter stays
unchanged. For a dynamic array the pass by value passes the pointer
held in the array variable. If you modify an item in the array inside
the method you end up changing the value in the *original* array you
passed, since dynamic arrays do not have the copy-on-write semantics
Delphi uses for String types (dynamic arrays do not have a reference
count, unlike strings).

 


-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://codenewsfast.com
http://groups.google.com
0
Peter
6/24/2011 8:37:18 AM
Ok, thanks guys.

That just confirms something in my head.

In my old app, there were 15 possible additions/deductions that the user could define and were stored as part of the user record. I used to read them into an array 15 elements long. It was also easy for reporting - if, for example, addition/deduction number 3 had been used then the report had a 15 element array and the value was added to the third element. Then, at the end, I could print out that addition/deduction no. 3 was used and had a total value of XX.

In my infinite wisdom when I started re-writing the app, I've decided to put additions/deductions in their own table so the user isn't restricted to 15 choices and having to clear an old one to make room for a new one. Now when it comes to my reports I've got a headache in that I no longer have a pre-defined array as user1 may only use 5, whilst user2 uses 20 etc.

Anyway.

Diolch yn Fawr.

Sion
0
Sion
6/24/2011 1:15:15 PM
<Sion Jones> schrieb im Newsbeitrag news:372215@forums.embarcadero.com...
....
> In use is there much difference between a dynamic and a static array?
....
You can declare an
a : Array[5 .. 8] of ...,
but AFAIK an
a : Array of ...
is always an Array[0 .. high(a)]

kind regards
Heiner
0
Heinrich
6/24/2011 9:50:44 PM
<Sion Jones> wrote in message news:372215@forums.embarcadero.com...

> If I call SetLength on a dynamic array, does it
> become more or less a static array?

In that it allocates a single block of contigious memory, yes.

> In use is there much difference between a dynamic
> and a static array?

A dynamic array is always allocated on the heap, and its size is specified 
at runtime.  A static array is allocated on either the stack or the heap, 
depending on how you are using it, and its size is specified at 
compile-time.

> due to a change of approach in one program I now need
> to declare an array without knowing how many elements
> it will have.

Then you have to use a dynamic array.

-- 
Remy Lebeau (TeamB)
0
Remy
6/25/2011 6:18:30 AM
Sion Jones wrote:

> If I call SetLength on a dynamic array, does it become more or less a
> static array?

"Dynamic" means that you canchange the size, and that is done using
SetLength. Dynamic arrays are merely pointers to arrays allocated on
the heap. If you do a Setlength on a non-empty array, new memory will
be allocated, the contents of the old memory will be copied over to the
new memory block and the old one will be deallocated. The address of
the new memory is assigned to the dynamic array variable.

Static arrays, OTOH, are simply fixed size memory blocks. They are not
referenced by a pointer, they are accessed directly.

http://rvelthuis.de/articles/articles-pointers.html#dynarrays

> In use is there much difference between a dynamic and a static array?

Can't say it is much, but therer are important differences. See the
article above.

-- 
Rudy Velthuis

"We must question the story logic of having an all-knowing
 all-powerful God, who creates faulty humans, and then blames
 them for his own mistakes."
 -- Gene Roddenberry
0
Rudy
6/26/2011 5:26:30 AM
Reply:

Similar Artilces:

Arrays, Arrays, Arrays
I mainly come from a JScript classic ASP background and I'm liking both C#.net and JScript.net. Now I have to say... "What's happened to arrays?" They used to be so flexible and easy to use. Here are my quick questions... Am I allowed to create an array with a non specified length so that I can add to it? If not then how can I modify an array's length? Can I create an array with a named index eg myArray["apple"] = "tasty"; or is the hash table the equivalent for this now? The classic array methods and properties like pop(), push(), length etc... are ...

problem with dynamic array of dynamic array
var a:array of array of integer; begin setlength(a,2); setlength(a[0],1); setlength(a[1],2); a[0,0]:=1; //it is ok a[0,1]:=2; //it is not ok in my understanding a[0,1]:=2 is not ok because a[0] has only one element as i declared setlength(a[0],1) so i dont understand why this code exemple is compiled and run just fine ? > {quote:title=Yossi Rozenberg wrote:}{quote} > in my understanding a[0,1]:=2 is not ok because a[0] has only one element as i declared setlength(a[0],1) so i dont understand why this code exemple is compiled and run just fine ? By default, acces...

How to unshift to an array in an array of arrays?
How to unshift to an array in an array of arrays? > -----Original Message----- > From: Angerstein [mailto:ang@nmc-m.dtag.de] > Sent: Wednesday, August 28, 2002 6:12 AM > To: beginners@perl.org > Subject: How to unshift to an array in an array of arrays? > > > How to unshift to an array in an array of arrays? $ perl -d -e 1 ..... DB<1> @aoa = ([qw(foo bar baz)]) << create array of arrays DB<2> x @aoa 0 ARRAY(0x80f82dc) 0 'foo' 1 'bar' 2 'baz' DB<3> unshift @{$aoa[0]}, 'qu...

Concatenating arrays from arrays of arrays
Hello - I am trying to accumulate items in a flat list (array) by concatenating on to it the scalars contained in arrays which in turn are contained in arrays. The data is to be read in from a file rather than existing as literals in my code. Everything I try either crashes the computer or gives me an array of arrays instead of a flat list. I have been programming in Unicon which has a concatenate operator, but Perl makes very heavy weather of nested list management. Should I switch to Lisp, or is there a way of doing what I want to do in Perl? Regards _John Sampson_ On...

referring to an array in an array of arrays
How can I get at an array in an array of arrays? I'm creating the arrays using: $fComments[$i++] = [ @comments ]; Now I want to get back what I put in: print "$fComments[0]\n"; but this prints ARRAY(0x107671f0). How do I follow that pointer? (I really think we need a pre-beginners list... =) TIA. - B You can try something like this: print "@{$fComments[0]}\n"; Which will attempt to dereference the array at $fComments[0]. -----Original Message----- From: Bryan R Harris [mailto:Bryan_R_Harris@raytheon.com] Sent: Tue...

An array of arrays
Could anybody help me out here? Here's the message I just got from a guy I know: ----------- I was wondering if you know how to create and access an array of arrays in Perl. Basically I'm parsing an exported text file containing e-mails and I think I want to move some fields into arrays until I'm ready to do my report. Here basically is what I'm looking at doing: ^ | | Array 1 (messages) | | v <------ Array 2 -------> (message fields:FROM, TO, CC, etc, sensitivity, etc) I want to be able to search through the first array to count how many messag...

Array of arrays
Hi I'm trying to return from a web service a array of arrays, one of the arrays is a array of objects, which I tested separately and it's working fine, and another array of integer and another of strings. When I run the web service I got this:You must implement a default accessor on System.Array because it inherits from ICollectionDefault accessor what that means? Thank you very much.  As Heinlein said "specialization is for insects" explore beyond the universe. Could you pls post the relevant code ? hi rushan thank you for the reply the code is here:  ...

array of arrays
Hi. =20 Im using a multidimensional array in the following way: $array[$i][$j][$k] =3D [ @list ]; where, @list is a 2-D array ie. = $list[][]. How do I access the individual elements of the array @list once Ive put it = into the array @array? Thanks. -ty On Thu, Oct 18, 2001 at 02:28:41PM -0600, Tyler Cruickshank wrote: > $array[$i][$j][$k] = [ @list ]; where, @list is a 2-D array ie. $list[][]. > > How do I access the individual elements of the array @list once Ive put it > into the array @array? $array[$i][$j][$k][$l][$m] eq $list[$l][$m] Howev...

array of arrays
Hi, I have a simple question: how do I access an array in an array? @array = (@array1, array2); ### unless this is wrong... the idea behind it is, that I have a long list, and want to put all 25 entries in a seperate array in that big array. later on I want to be able to read one specific array at a certain position... -- cheers alex mailto:Keex@gmx.de you need to learn about references. read the: perldoc perllol here is the jist though. @all = (\@array1, \@array2 ... ); # ... so on to access first array and first element...

Array of Array.
How is possible to declare a DYNAMIC array of array ? I need something like a dynamic hash table whose pages grow up dynamically too (each one can have different number of elements). In other words, a matrix that can be dynamic in both dimension. example: [1,2,3...] [3,4..] [5] [6,7,8,9...] [.] .. .. .. Have you considered using a DataStore? -- <hopethishelps/> Roy Kiesler [TeamSybase] SDN CodeXchange -- http://codexchange.sybase.com "Marco Meoni - News" <mmeoni@usl4.toscana.it> wrote in message news:401ba036$1@forums-2-dub... > How ...

MOVED: problem with dynamic array of dynamic array
Moved to: embarcadero.public.delphi.language.delphi.general or: https://forums.embarcadero.com/thread.jspa?threadID=57076 -- Rudy Velthuis Rudin's Law: In a crisis that forces a choice to be made among alternative courses of action, people tend to choose the worst possible course. ...

[@array] and \@array
Given the case, my @array = (1,2,3,4); my $var1 = [@array]; my $var2 = \@array; What's the difference between $var1 and $var2? Are they all refered to @array? Thanks guys. On 3/22/07, Jm lists <practicalperl@gmail.com> wrote: > Given the case, > > my @array = (1,2,3,4); > my $var1 = [@array]; > my $var2 = \@array; > > What's the difference between $var1 and $var2? > Are they all refered to @array? > Thanks guys. $var2 is a reference to @array. $var1 is a reference to new array. Observe #!/usr/bin/perl use strict; use war...

Array of Arrays
Hi, I am looking for a way to create an array of arrays which needs to be passed to Oracle database to minimize the round trips to hte server. I was successful in sending simple arrays so far but no idea on how to create array of arrays in PB. I have used structures to simulate this but looks like I can't send the PB structure to Oracle. Anyone has any suggestions? Thanks, Anil PB 11 or 12 I believe will have this native. <Anil> wrote in message news:422789a4.721c.1681692777@sybase.com... > Hi, > > I am looking for a way to create an array of ar...

Array of Arrays
Hi, I am looking for a way to create an array of arrays which needs to be passed to Oracle database to minimize the round trips to hte server. I was successful in sending simple arrays so far but no idea on how to create array of arrays in PB. I have used structures to simulate this but looks like I can't send the structure to Oracle. Anyone has any suggestions? Thanks, Anil Please don't cross-post. On 3 Mar 2005 14:02:47 -0800, Anil wrote: >Hi, > >I am looking for a way to create an array of arrays which >needs to be passed to Oracle database t...

Web resources about - Dynamic Arrays / Static Arrays - embarcadero.delphi.general

Dynamics - Wikipedia, the free encyclopedia
Brownian dynamics , the occurrence of Langevin dynamics in the motion of particles in solution (e.g. a grain in water, as was first seen by Brown); ...

Datacenter Dynamics
Datacenterdynamics for data center information, conferences, news, videos, whitepapers and reports globally

Dynamic Chiropractic - chiropractic, news, articles, research & information for chiropractors - Find ...
Dynamic Chiropractic - chiropractic, news, articles, research & information for chiropractors - Find a Chiropractor

Walmart's Everyday Low Prices Face Amazon's Dynamic Price Push
... low prices" strategy is becoming an everyday price-adjustment strategy. In what seems to be a direct response to Amazon's sophisticated dynamic ...

Mea Culpa: Confidence Proceedings Edition: I Really Do Think Dynamic Scoring Is a Bad Idea
... *Brad DeLong noted that when he was a Treasury political appointee, one of the Treasury career staff economists lectured me him about dynamic ...

dynamic billboards to up your photo game
NYC is one of the most photographed places in the world, especially this time of year. And while Instagram has made every one of us feel like ...

SNL perfectly captured the dynamics of the Republican presidential race
The "Saturday Night Live" recap of the latest Republican presidential debate reflected fairly accurately the state of the GOP primary. Beck ...

Yolanda Foster explains her divorce: ‘chronic illness changed our dynamic’
... is extremely challenging, not only for the person that’s sick, but also for the caregiver,” Foster admitted. “It for sure changed the dynamic ...

CES 2016 TV tech: 4K yawns, high dynamic range dawns
... Show in Las Vegas this January, TVs with 4K resolution are old hat. So what's new hat? The next-generation TV technology known as high dynamic ...

Boston Dynamics wishes you a terrifyingly happy holiday
Whether you think holiday greeting cards are touching or annoying, Boston Dynamics is celebrating the season just a little differently with this ...

Resources last updated: 12/24/2015 8:14:20 PM