Re: Pointers
-- With all the talk of pointers, the following may save someone some
--trouble. Bottom line, in a numerical sequence, the storage space is
--somewhar unpredictable...so forget about pointer arithmetic.
-- A small demo prog follows showing the bytes per term may change
--unexpectdly as the data varies from float to float auto converted to
--integer whae possible.
--Demo for size of a numeric sequence.
--include bytes.e
--This handy bytes.e came from Rob Craig. It estimates the # of bytes needed
--by an object. I have found it useful.
--Bytes.e
global function bytes(object x)
-- estimates the number of bytes of storage needed for any
-- Euphoria 2.0 data object (atom or sequence).
integer space
if integer(x) then
return 4
elsif atom(x) then
return 16
else
-- sequence
space = 24 -- overhead
for i = 1 to length(x) do
space = space + bytes(x[i])
end for
return space
end if
end function
-->integer int
-->int = 1
--? bytes_needed(1) -- 4
--? bytes_needed(1.5) -- 16
--? bytes_needed({1,2,3}) -- 36
--? bytes_needed({{1.5,2.5}, {1,2}}) -- 112
--? bytes_needed({{1,2},{}, {}, {1,2}}) -- 88 , 112, 136
--? bytes_needed(int)
--END bytes.e
--Make a seq of floats with howMany elements
sequence seq1
integer howMany
howMany = 10
seq1 = repeat(1.1, howMany)
--Set values all different
for i = 1 to howMany by 1 do
seq1[i] = seq1[i] * i
end for
? bytes(howMany) --IE, an integer uses 4 bytes
? bytes(1.1) --IE, a float uses 16 bytes
? bytes(seq1) --A seq uses 24 + 4 per integer + 16 per float
? seq1 --Note, autoconversion to integer for term 10
? (bytes(seq1) - 24) / howMany --IE, a numerical seq length is not predictable
--just a check
? integer(seq1[9])
? integer(seq1[10])
--Pointer arithmetic for a numeric sequence not a good idea.
--Arthur P. Adamson, The Engine Man, euclid at isoc.net
|
Not Categorized, Please Help
|
|