[solved]Help with UDT and cptr

General FreeBASIC programming questions.
Post Reply
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

[solved]Help with UDT and cptr

Post by SamL »

im using FreeBASIC-1.10.0-winlibs-gcc-9.3.0
Some of my older code is not working i think its because I updated the compiler.
I'm copying UDT values into allocated memory.
then i want one of the values from the memory i would just use:

Code: Select all

some_var = *cptr(UDT ptr, some_pointer).some_UDT_fieldname
but this no longer works, i did find another way to get it to work but requires dimming the type again, i would like to avoid that if possable.

My question is: Is there a way to get the UDT value from the allocated memory without dimming a udt but only casting as the UDT like in the example below. or maybe there is a new way to do what i'm doing in the function no_longer_works() ?

here is some example test code:

Code: Select all

type UDT
    as ulongint a
    as ulongint b
end type
'memory to hold data
dim as byte ptr p = CAllocate(1, SizeOf(UDT))

'UDT for inital data, add some values
dim as UDT test_udt
test_udt.a = 18446744073709551615
test_udt.b = 10101010101010101010

'copy the UTD into the allocated memory
*cptr(UDT ptr, p) = test_udt 

'Clear the UDT values, because they are now in allowcated memory
test_udt = type<UDT>(0) 'Clear test_udt values to 0 
print "test_udt.a = "; test_udt.a
print "test_udt.b = "; test_udt.b


'function to read some data from the memory 

'function no_longer_works( p as byte ptr) as ulongint
'    return *cptr(UDT ptr, p).b
'end function

function works( p as byte ptr) as ulongint
    dim as UDT new_UDT
    new_UDT = *cptr(UDT ptr, p)
    return new_UDT.b
end function


'print "value from memory = "; no_longer_works(p)
print "value from memory = "; works(p)

Deallocate(p)
sleep
SamL
Last edited by SamL on Mar 01, 2024 20:21, edited 1 time in total.
srvaldez
Posts: 3390
Joined: Sep 25, 2005 21:54

Re: Help with UDT and cptr

Post by srvaldez »

you need to use parenthesis

Code: Select all

function works( p as byte ptr) as ulongint
    return (*cptr(UDT ptr, p)).b
end function
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

Re: Help with UDT and cptr

Post by SamL »

yup that fixed it!!
Thank you!
SamL
fxm
Moderator
Posts: 12181
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [solved]Help with UDT and cptr

Post by fxm »

For more information, see the closed bug report:
#811 *(ptr).field should give an error
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

Re: [solved]Help with UDT and cptr

Post by SamL »

ok, that explains it, :)
Post Reply