beware with array

beware with array

Postby fafi » Thu Feb 21, 2013 12:08 am

Please test, what is your opinion :

Regards
Fafi

Code: Select all  Expand view

#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
fafi
 
Posts: 169
Joined: Mon Feb 25, 2008 2:42 am

Re: beware with array

Postby Daniel Garcia-Gil » Thu Feb 21, 2013 11:39 am

Hello

is perfectly correct...

do you see any problem?
User avatar
Daniel Garcia-Gil
 
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita

Re: beware with array

Postby fafi » Thu Feb 21, 2013 12:10 pm

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
fafi
 
Posts: 169
Joined: Mon Feb 25, 2008 2:42 am

Re: beware with array

Postby Otto » Thu Feb 21, 2013 6:24 pm

Mr. Rao explained how arrays work here:
viewtopic.php?f=3&t=25699

Thank you Mr. Rao.

Best regards
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6072
Joined: Fri Oct 07, 2005 7:07 pm

Re: beware with array

Postby nageswaragunupudi » Fri Feb 22, 2013 12:53 am

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: 10313
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: beware with array

Postby nageswaragunupudi » Fri Feb 22, 2013 1:04 am

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
User avatar
nageswaragunupudi
 
Posts: 10313
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 46 guests