Page 1 of 1

beware with array

Posted: Thu Feb 21, 2013 12:08 am
by fafi
Please test, what is your opinion :

Regards
Fafi

Code: Select all

#include "fivewin.ch"

function main()

local aOne := {"One"}

local aTwo := aOne

local nOne := 1

local nTwo := nOne

local cOne := "One"

local cTwo := cOne

for i := 1 to len(aOne)
    ?aOne[i] + aTwo[i]
next

aOne[1] := "Good"
for i := 1 to len(aOne)
    ?aOne[i] + aTwo[i]
next

aTwo := aclone(aOne)

for i := 1 to len(aOne)
    ?aOne[i] + aTwo[i]
next

aOne[1] := "Bed"
for i := 1 to len(aOne)
    ?aOne[i] + aTwo[i]
next

?str(nOne,1)+str(nTwo,1)

nOne := 3
?str(nOne,1)+str(nTwo,1)

?cOne+cTwo

cOne := "Hello"

?cOne+cTwo

aTwo := aOne

for i := 1 to len(aOne)
    ?aOne[i] + aTwo[i]
next

aOne[1] := "FWH"
for i := 1 to len(aOne)
    ?aOne[i] + aTwo[i]
next

return nil
 

Re: beware with array

Posted: Thu Feb 21, 2013 11:39 am
by Daniel Garcia-Gil
Hello

is perfectly correct...

do you see any problem?

Re: beware with array

Posted: Thu Feb 21, 2013 12:10 pm
by fafi
Yes.. , when I used array..I have been having problems
But after investigation, I change it with Aclone.. the problems solved..

Regards
Fafi

Re: beware with array

Posted: Thu Feb 21, 2013 6:24 pm
by Otto
Mr. Rao explained how arrays work here:
http://forums.fivetechsupport.com/viewt ... =3&t=25699

Thank you Mr. Rao.

Best regards
Otto

Re: beware with array

Posted: Fri Feb 22, 2013 12:53 am
by nageswaragunupudi
Please read the posting as advised by Mr. Otto.

Now let us see how character strings are handled in memory.

Let us see the example

a := "hello"
b := a

Now both the variables a and b contain pointer to the same memory location where the characters "hello" are stored.

Let us operate on one variable 'a'.

a := Upper( a )
Now the value "hello" is converted to upper case and the result "HELLO" is stored in a different memory location and the pointer to that location is assigned to the variable 'a'.

Now, variable a contains the pointer to the location where "HELLO" is stored in the memory and variable b continues to contain the pointer to the location where "hello" is stored.

This behaviour of character vars and array vars has been like this consistently since Clipper was born and we need to keep this in mind.

Re: beware with array

Posted: Fri Feb 22, 2013 1:04 am
by nageswaragunupudi
This also brings us to another issue. Too many string operations result in fragmentation of memory and string operations are slower.
In the olden days when memory was limited and computers were slower, we had to consciously keep the string usage and operations to the minimum to achieve faster execution speeds and also to conserve contiguous memory, though Clipper ( and now (x)Harbour ) does the house keeping of garbage collection frequently.

With the increased memory and speeds of the present day computers the issue does not seem to be important. Nonetheless, we better keep this in mind while programming. Use numeric fields and values wherever possible instead of character values and avoid unnecessary string operations.