beware with array

Post Reply
User avatar
fafi
Posts: 169
Joined: Mon Feb 25, 2008 2:42 am

beware with array

Post 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
 
User avatar
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: beware with array

Post by Daniel Garcia-Gil »

Hello

is perfectly correct...

do you see any problem?
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
User avatar
fafi
Posts: 169
Joined: Mon Feb 25, 2008 2:42 am

Re: beware with array

Post 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
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: beware with array

Post by Otto »

Mr. Rao explained how arrays work here:
http://forums.fivetechsupport.com/viewt ... =3&t=25699

Thank you Mr. Rao.

Best regards
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: beware with array

Post 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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: beware with array

Post 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.
Regards

G. N. Rao.
Hyderabad, India
Post Reply