Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / can DBP pass UDTs OUT of functions...?

Author
Message
DrakeX
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location:
Posted: 27th Aug 2003 06:27 Edited at: 27th Aug 2003 06:39
DBP finally recieved the ability to pass them into functions. but somehow it doesn't seem that it can pass them OUT. sadly, i find the latter to be more useful.

here is a bit of test code..



if i try to run it i get an error saying that i must use the same usertype. well i am! there's only one usertype to use!

so either there's something i'm not doing right, or it's passing a pointer back from the function -- and not a type.

[edit]just tried something -- it DOES pass back an integer and not a type.

in fact, it passes back the x field of the type.

how useful

[edit again]and i think i've figured out why. the ENDFUNCTION and EXITFUNCTION commands don't seem to have support for UDTs yet. they're probably just doing a cast to an integer on the type pointer. since the first field is x, the value at the pointer will be the value of x.

and testing some more, with different sizes of UDT fields, like WORD, this seems to be the case.

sigh.

i'm looking at yooooou!
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 27th Aug 2003 11:17
Well, there's no real need to pass them out. If passing in is by reference, this does what your after.

Here's how PB handles it..



Kevin Picone
Play Basic - Visible Worlds - Kyruss II
[url]www.underwaredesign.com[/url]
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 27th Aug 2003 15:24
Unfortunately, all variables are passed into functions by value
OSX Using Happy Dude
22
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 27th Aug 2003 16:18
The alternative way would be to use allocated memory...


DrakeX
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location:
Posted: 27th Aug 2003 18:02
"there's no real need to pass them out"

why not? how about

a=updatethinger(a)

so that the values of a are passed into it, the function does whatever it needs to do, and passes back the updated a so a now holds the new variables?

or how about creation functions, where you don't want to assign all the type fields manually, like

a as mytype
a=NewThinger(5,10,"Bob")

passing them out is very useful.

"all variables are passed into functions by value"

actually that's not really what i'm looking for. i'm looking to pass a type OUT.

what i was thinking was i could use a global return thing... like this:



this works but it's pretty workaround-y.

what i would like even better would be the ability to pass types by reference; kinda like some other language

stop looking at me!
APEXnow
Retired Moderator
23
Years of Service
User Offline
Joined: 15th Apr 2003
Location: On a park bench
Posted: 27th Aug 2003 18:09 Edited at: 27th Aug 2003 18:09
This whole problem would be solved if you (could) pass thr UDT into the function by reference. Yeah, in some cases you would need to create a temporary variable if you're not wanting to modify the original variable but still, it saves having to copy one to the next etc.

empty
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 27th Aug 2003 18:12 Edited at: 27th Aug 2003 18:13
Quote: " "there's no real need to pass them out"

why not? how about

a=updatethinger(a)

so that the values of a are passed into it, the function does whatever it needs to do, and passes back the updated a so a now holds the new variables?"

Quote: "what i would like even better would be the ability to pass types by reference; kinda like some other language "


Isn't that exactly what Kevin suggested?

If UDTs can be passed to functions by reference (like in Kevin's PlayBasic example code) you wouldn't necessarily need the option to get UDT back from functions.

I awoke in a fever. The bedclothes were all soaked in sweat.
She said "You've been having a nightmare and it's not over yet"
OSX Using Happy Dude
22
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 27th Aug 2003 18:28
As Ian has said before, they're passed by value - doubtful that Lee will change it...


Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 27th Aug 2003 23:23
Ian:

You must kidding ?. So just what are they thinking ?. Let me guess 'it's so it make it easier to understand ?.



Drake:

Well, as I previously said. Passing data structures is really the domain of pointers (which is obivous enough). In passing the data by value, he's pretty much made the act of passing the structures pretty useless. Every call it's shifting all the fields. Imagine if the type has a heavy byte count.. Ouch. Your only option is to shuffle data around to get it back. That's a lot of pointless overhead.

In PB what your after is trivial.

Dim A as MyType
Dim B as MyType
NewThing(A.MyType, 5,10,"Bob")
NewThing(B.MyType, 55,110,"Bill")

Kevin Picone
Play Basic - Visible Worlds - Kyruss II
[url]www.underwaredesign.com[/url]
Toothpick
23
Years of Service
User Offline
Joined: 9th Feb 2003
Location:
Posted: 28th Aug 2003 00:04
Whats wrong with keeping them global?
DrakeX
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location:
Posted: 28th Aug 2003 00:19 Edited at: 28th Aug 2003 00:21
"Whats wrong with keeping them global?"

weeeelll.... globals don't always solve problems. often you want something to be local, and global is not an option.

"In PB what your after is trivial."

i know that kevin you don't have to keep telling me.

well since i've decided to create a thing that imitates Blitz's type system, and it'll be using a dynamic array anyway, and arrays are global, it won't much matter.

but i'd still like to be able to pass things (regular variables too) by reference. or at the very least, be able to pass UDTs out.

cause i mean come on, if we can get the address of arrays, why not other kinds of variables?

stop looking at me!
OSX Using Happy Dude
22
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 28th Aug 2003 00:25
What you can also do (which may work), is get hold of IanM's ARRAY plug-in, pass the address of the structure your interested in and modify it my peeking/pokeing memory...


DrakeX
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location:
Posted: 28th Aug 2003 01:04
i actually do have the array plugin but the problem is that i can't get the address of individual type instances. if they're in an array it's no problem; but something like

a as mytype
dosomething(a)

don't work. because there is no AddressOf() function.

stop looking at me!
OSX Using Happy Dude
22
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 28th Aug 2003 01:36
Hmm - I got it to work with indiviual instances. Ah, no I didn't - used an array of 1...


MushroomHead
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 28th Aug 2003 01:42 Edited at: 28th Aug 2003 01:47
Quote: "i actually do have the array plugin but the problem is that i can't get the address of individual type instances. if they're in an array it's no problem; but something like"


Just bung the lot into a memblock, then easy to work out addresses ... yeah I know it's a long winded way ...

How about declaring your types in a DLL then access and modify it inside the DLL (remember a DLL runs in it's own memory space) ... will work pretty well as long as you don't go trashing the memory
DrakeX
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location:
Posted: 28th Aug 2003 03:04 Edited at: 28th Aug 2003 03:24
"Just bung the lot into a memblock, then easy to work out addresses"

hehe

"How about declaring your types in a DLL then access and modify it inside the DLL"

seems really workaroundy.

oh well. i'll just live with arrays of types til then.

i've also emailed IanM to see if he knows anything about passing UDTs into TPC functions.. right now with commands like END/EXITFUNCTIOB and PRINT, DBP simply does an integer cast on the type. so

type a
x
endtype

a as mytype
a.x=4
print a

prints "4". maybe there is a way to pass UDTs into TPC functions by using a void*. but then, what type of parameter would i use in the TPC stringtable? perhaps X like in the CALL DLL stringtables? hmmm...... questions........

i'd like to know if there is a way to, because if there is, i'd be able to make a AddressOf function that would allow you to pass the pointer of the UDT into a function -- thereby allowing byref. however the docs say it's not possible to pass UDTs, but who knows..

[edit]well i tried this:

EXPORT DWORD TryMe(BYTE* ptr)
{ return ReadAddress<BYTE>((DWORD)ptr); }

inside IanM's utility DLL. i added the command to the stringtable using X as the input parameter. DBP no longer whines about incorrect parameters -- however it still doesn't work. it just craps out and gives no error, no memory violation, no anything.

i also just tried it by passing in a memblock pointer -- and it works fine. so it IS DBP's fault that the UDT is not getting passed in correctly. dammit.

stop looking at me!
MushroomHead
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 30th Aug 2003 12:53
Quote: "[edit]well i tried this:

EXPORT DWORD TryMe(BYTE* ptr)
{ return ReadAddress<BYTE>((DWORD)ptr); }"


The above won't work, I'm sure I have tried something similar to that before and was getting nonsense return values. It's best to have an ADR() function inside DBP rather than writing some hack to get it working externally, the reason (in WinXP's case), is all programs on NT machines run in their own protected memory space to prevent people from trashing another process or over writing memory locations ...
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 30th Aug 2003 14:26
And the answer is ... no but he is thinking about it, along with sizeof() and addressof() (although 'byref' would be a better choice).

Login to post a reply

Server time is: 2026-07-25 06:42:22
Your offset time is: 2026-07-25 06:42:22