pointer to UDT type - why doesn't this work? - solved

General FreeBASIC programming questions.
Post Reply
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

pointer to UDT type - why doesn't this work? - solved

Post by speedfixer »

Code: Select all

data "Theatre Powder Rose",226,212,212,_
    "Lotus Pink",234,208,214,_
    "Ice Pink",241,225,224,_
    "Cake Frosting",249,223,229,_
    "Arrowhead White",249,234,235

type colorlist_type
    n as long
    s as string
    r as ubyte
    g as ubyte
    b as ubyte
end type

'common shared as colorlist_type ptr ru_cnlist
dim shared as colorlist_type ptr ru_cnlist

sub ru_colorretrieve()

dim as long ndx
dim as colorlist_type colornamelist(0 to 5)

ndx = 0
do
    ndx += 1
    colornamelist(ndx).n = ndx
    read colornamelist(ndx).s
    read colornamelist(ndx).r
    read colornamelist(ndx).g
    read colornamelist(ndx).b
loop while ndx <> 5

print colornamelist(ndx).n; " + "; colornamelist(ndx).s; " + "; colornamelist(ndx).r;_
         " + "; colornamelist(ndx).g; " + "; colornamelist(ndx).b

ru_cnlist = @colornamelist(0)

    print ru_cnlist[5].n; " + "; ru_cnlist[5].s; " + "; ru_cnlist[5].r;_
             " + "; ru_cnlist[5].g; " + "; ru_cnlist[5].b

print " *** end sub ***"
sleep

end sub                                                 ' ru_colorretrieve

dim as long nnn
dim as ubyte cr, cg, cb
dim as string cname

print "before"
ru_colorretrieve()
print "after"

' Arrowhead White --r 249 --g 234 --b 235

nnn = ru_cnlist[5].n
cname = ru_cnlist[5].s
cr = ru_cnlist[5].r
cg = ru_cnlist[5].g
cb = ru_cnlist[5].b

print " index: "; nnn
sleep
print " name: "; cname, " --len: "; len(cname)
sleep
print " red: "; cr
sleep
print " green: "; cg
sleep
print " blu: "; cb
print
using:
FreeBASIC Compiler - Version 1.07.0 (05-20-2019), built for linux-x86_64 (64bit)

Notice cname length with and without [ -exx ]

david
Last edited by speedfixer on Mar 13, 2020 23:52, edited 1 time in total.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: pointer to UDT type - why doesn't this work?

Post by badidea »

colornamelist does not exist outside ru_colorretrieve()
So pointer ru_cnlist points to invalid memory.

If you move colornamelist outside the sub, it will work, e.g:
dim shared as colorlist_type colornamelist(0 to 5) '1 to 5?
But there are cleaner solutions then dim shared.
Last edited by badidea on Mar 13, 2020 23:52, edited 1 time in total.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: pointer to UDT type - why doesn't this work?

Post by speedfixer »

OK
Forgot to make the array static.

Thanks
SeaVipe
Posts: 28
Joined: Dec 22, 2015 19:13
Location: Western Canada
Contact:

Re: pointer to UDT type - why doesn't this work?

Post by SeaVipe »

badidea wrote:But there are cleaner solutions then dim shared.
Hello @badidea, What would a "cleaner solution to Dim Shared" be?
Thanks.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: pointer to UDT type - why doesn't this work?

Post by badidea »

SeaVipe wrote:Hello @badidea, What would a "cleaner solution to Dim Shared" be?
Thanks.
Hi, I should have written 'there are other solutions than Dim Shared, which could be cleaner'. The part that Speedfixer posted seems part of a larger program so a bit difficult to determine what is best. But for the example as posted there is no need for a shared (or global) variable. I would only use a shared variable if a lot of parts in the program need access to it (read or write). If I share my wallet with some friends or the whole world, it can make certain things easier, but it is also more dangerous for me.
Speedfixer did chose a different solution: static list in sub, with shared pointer to it. But that seems a bit weird solution to me. I would write the code above (without changing functionality) as:

Code: Select all

const NUM_COLORS = 5

data "Theatre Powder Rose",226,212,212,_
	"Lotus Pink",234,208,214,_
	"Ice Pink",241,225,224,_
	"Cake Frosting",249,223,229,_
	"Arrowhead White",249,234,235

type colorlist_type
	as long n
	as string s
	as ubyte r, g, b
	declare operator cast () as string
end type

'allow to print a colorlist_type
operator colorlist_type.cast () as string
	return str(n) & " + " & s & " + " & r & " + " & g  & " + " & b
end operator

'below, index 0 not is used as in original post
dim as colorlist_type colornamelist(0 to NUM_COLORS)
dim as colorlist_type ptr ru_cnlist = @colornamelist(0)

sub ru_colorretrieve(colornamelist() as colorlist_type)
	for i as integer = 1 to ubound(colornamelist)
		colornamelist(i).n = i
		read colornamelist(i).s
		read colornamelist(i).r
		read colornamelist(i).g
		read colornamelist(i).b
	next
end sub

ru_colorretrieve(colornamelist()) 'pass array (as reference)

print colornamelist(NUM_COLORS) 'show last item in array
print ru_cnlist[NUM_COLORS] 'show last item in array via pointer

print "--- ru_cnlist[5] ---"
with ru_cnlist[5]
	print " index: "; .n
	print " name: "; .s, " --len: "; len(.s)
	print " red: "; .r
	print " green: "; .g
	print " blu: "; .b
end with
sleep
But their are many ways and personal preferences.
SeaVipe
Posts: 28
Joined: Dec 22, 2015 19:13
Location: Western Canada
Contact:

Re: pointer to UDT type - why doesn't this work? - solved

Post by SeaVipe »

Thank you for the clarification, much appreciated.
Post Reply