pointers to UDT?

New to FreeBASIC? Post your questions here.
Post Reply
maurosebastian91
Posts: 25
Joined: Mar 21, 2021 18:22

pointers to UDT?

Post by maurosebastian91 »

Hello partners.

I'm here today to ask you about UDTS pointers, which I've already read a lot about, I've tried a lot but it doesn't work for me.

remember my UDT sounds?

Code: Select all

Type sonidos
	Dim As Byte Ptr memoria
	dim buffer as sfSoundBuffer ptr
	dim sonido as sfSound ptr
End Type
what I am looking for, is to create a pointer to that UDT, to use in other subs and other modules.
I have already tried the following:

Code: Select all

var sound1 = new sonidos
and also

Code: Select all

dim sound1 as sonidos
dim son1 as sonidos ptr = @sound1
it only works inside modules but not in subs or functions, nor in other modules.
How can I solve this little problem?
what am I doing wrong?

Thank you,

Greetings.
speedfixer
Posts: 583
Joined: Nov 28, 2012 1:27
Location: California
Contact:

Re: pointers to UDT?

Post by speedfixer »

You need to understand SHARE and probably COMMON.

david
badidea
Posts: 2521
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: pointers to UDT?

Post by badidea »

What do you mean with 'does not work in subs/functions? How did your sub of function look like? What did you try?

Note that the 'var' keyword can be tricky if you do not know exactly what you are doing. It hides the the data type in the code.

With missing information, have a look at this:

Code: Select all

#define sfSoundBuffer any 'just to get get the code to compile ...
#define sfSound any '... without the real data types

Type sonidos
	Dim As Byte Ptr memoria
	dim buffer as sfSoundBuffer ptr
	dim sonido as sfSound ptr
End Type

sub print_address_1(soundPtr as sonidos ptr)
	print "Sub1: soundPtr point to address: " & soundPtr
end sub

sub print_address_2(byref sound as sonidos) 'byref is optional/default for udt
	print "Sub2: sound is at address: " & @sound
end sub

dim sound as sonidos
dim soundPtr as sonidos ptr = @sound
print "Main: soundPtr points to address: " & soundPtr
print_address_1(soundPtr)
print_address_2(sound)

var soundPtr2 = new sonidos
print "Main: soundPtr2 points to address: " & soundPtr2
print_address_1(soundPtr2)
print_address_2(*soundPtr2)
delete soundPtr2
Last edited by badidea on Jan 27, 2022 8:25, edited 1 time in total.
Munair
Posts: 1274
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: pointers to UDT?

Post by Munair »

maurosebastian91 wrote: Jan 24, 2022 23:48 and also

Code: Select all

dim sound1 as sonidos
dim son1 as sonidos ptr = @sound1
it only works inside modules but not in subs or functions, nor in other modules.
It works if you pass the UDT as a parameter to a sub or function, as badidea shows, OR if you declare your UDT instance as shared:

Code: Select all

dim shared sound1 as sonidos
dim shared son1 as sonidos ptr = @sound1
If you want to be able to also access the instance in other modules, I suggest you use extern:

Code: Select all

extern sound1 as sonidos
dim sound1 as sonidos

extern son1 as sonidos ptr
dim son1 as sonidos ptr
Last edited by Munair on Feb 13, 2022 17:55, edited 1 time in total.
caseih
Posts: 1971
Joined: Feb 26, 2007 5:32

Re: pointers to UDT?

Post by caseih »

maurosebastian91 wrote: Jan 24, 2022 23:48

Code: Select all

var sound1 = new sonidos
That should work fine inside a function or sub. That creates a new instance on the heap. sound1 is then a pointer, which you can return. You need to eventually delete it or you get a memory leak.

If you create an instance of sonidos on the stack inside a function, you cannot return a pointer to that instance because by the time the caller gets the pointer, the data on the stack is no longer valid. It may appear to function but the data being pointed at may be corrupted.
maurosebastian91
Posts: 25
Joined: Mar 21, 2021 18:22

Re: pointers to UDT?

Post by maurosebastian91 »

Thank you very much for your answers,
They were very useful to me.

a greeting.
Post Reply