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 / Variables acting very strangely

Author
Message
Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 17th Apr 2011 16:27 Edited at: 17th Apr 2011 16:31
I've been working on my project, and I've been using DBPro for a couple of years now but I've never seen this ever.

I have this function:


And I call it like this:


The object is created, but the variable ews_Parent usually equals 14872492, even though the value I've passed in was 1(EWS_UP==1).

I'm entirely baffled, any one have ideas why this is happening?


TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 17th Apr 2011 17:42
As far as I know, you can't pass constants into a function, they are global everywhere. I haven't tested, but you might want to try:



Also, I think you can shorten all of the code that puts the object into a group.

Change the constants to this:



And change the grouping code to this:



TheComet

Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 17th Apr 2011 20:14
Well constants are supposed to be replaced with their assigned value when the precompiler preps the source file for compilation.

I tried your way of simply putting the constant into a variable and then sending it to the function but it still didn't work.

Then I simplified my code like you suggested(that was its original form, but I was trying to isolate the bug), and it still didn't work so I thought about the order of the parameters in the function.

My parameters are: an integer, a UDT, a UDT, and then an integer.
So I changed to this order: an integer, an integer, a UDT, a UDT.
And it magically works!

Seems the two UDTs corrupt something in the passing of the last integer. Would be considered a bug?


tiresius
23
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 18th Apr 2011 04:28
What is VECTOR_ZERO ?


A 3D marble platformer using Newton physics.
Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 18th Apr 2011 05:08
It's just a global variable that I use when I want to pass a value of (0,0,0) to a function.




Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Apr 2011 23:45
Your code seems to work fine for me. Have you put the constant declarations and the function in different files?

Here's the code I used:

Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 19th Apr 2011 02:06 Edited at: 19th Apr 2011 02:08
Yes, the constants are declared in a separate source file(along with all the UDTs) and that function is declared in a separate source file.


Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Apr 2011 02:17
That could be your problem but I'm not sure why so hopefully someone else can clarify this for you. I believe globals can only be accessed by functions declared in the same file so perhaps something similar happens to constants. However, I can't immediately see why that should allow your code to work when the order is changed.

We probably need IanM to look at this thread.
Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 19th Apr 2011 04:03
Quote: "We probably need IanM to look at this thread. "


Preferably.

Quote: "I believe globals can only be accessed by functions declared in the same file so perhaps something similar happens to constants."

That would be news to me.


WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 19th Apr 2011 04:35
I'm thinking that it might be the order the files are added together when compiling. Usually you would expect constants and UDT's to be at the head of the source but DBP (as far as I know) attaches other source files to the end of the main file.

I've noticed that the #include directive isn't required anymore in DBP (in a project anyway) so you cannot order your separate source files. Maybe this is why the defines etc are parsed at the end of the source or somewhere in the middle.

Warning! May contain Nuts!
Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 19th Apr 2011 21:49
Do you not have to GOSUB to the constant definitions?

Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 20th Apr 2011 00:12
Quote: "Do you not have to GOSUB to the constant definitions?"

No, constants are found by the precompiler and the values that are assigned to them are replaced wherever the constant is found in the source file.

Quote: "I've noticed that the #include directive isn't required anymore in DBP (in a project anyway) so you cannot order your separate source files. Maybe this is why the defines etc are parsed at the end of the source or somewhere in the middle."

I'm using CodeSurge v1.0 and it allows you to set the compile order(however it's pretty buggy) so I tried to put the source file that defines the constants and UDTs as far up the compile chain as I could, but not before the main source file.


Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 22nd Apr 2011 09:57
O_o

I think you should post this in bug reports


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Apr 2011 12:08
Ah, I think that I know this bug from long ago.

The problem seems to happen when you pass a UDT as a parameter into a function, and the UDT is defined after the function. But it doesn't happen all of the time - sometimes it works fine, and sometimes it throws out a compiler error instead.

I think that this was one of the reasons for my very specific layout and design of code libraries (linky) - It's so long now that I can't remember exactly.

I've tried to reproduce the problem this time too with the code that GG has posted, but haven't managed it - the best (worst?) I've done is to put the type definition after everything else and get a parameter mismatch compilation error when calling the function.

I suspect what's happening to Dar13 is that not enough stack space is being reserved for the UDT parameter within the function (4 bytes, instead of 12), so that everything defined after it is being shifted left, and that the value he is getting from the integer is actually the third part of the first UDT (the float representation mapped into an integer).

Dar13, if you can locate and send me the .dbm file from your compilation of this program in it's non-working state, I can tell you if that is what is happening or if it's something else.

Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 22nd Apr 2011 21:38
I've run into this problem in the past, as has one of my padawans. As far as I can tell, you can't pass a UDT to a function. I thought this was an intended restriction, and used alternate techniques. It'd be nice if this turns out to be a bug and gets corrected at some point

Quote: "
Quote: "I believe globals can only be accessed by functions declared in the same file so perhaps something similar happens to constants."
That would be news to me.
"

Just wanted to clarify this one. A global declared in any one source file (main or include) is accessible in any other source file. I have a couple of source files that I routinely include in all of my new projects as I start them. They contain commonly used functions that I've written over the years that I find are useful in every project. Some of them require global definitions to function (as they inter-operate to some extent). Each include has an initialise function that declares the globals needed, and I call each of the initialise functions from my main source, first thing. These globals are accessible by the main source file as well as the functions in which they are declared.

Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 22nd Apr 2011 22:53
Quote: "Dar13, if you can locate and send me the .dbm file from your compilation of this program in it's non-working state, I can tell you if that is what is happening or if it's something else.
"

Email sent.

Quote: "I've run into this problem in the past, as has one of my padawans. As far as I can tell, you can't pass a UDT to a function. I thought this was an intended restriction, and used alternate techniques. It'd be nice if this turns out to be a bug and gets corrected at some point "

As long as I've used DBPro you could pass UDTs in, but not out. Not sure why, but that's how it's been since U5.6(link).


IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Apr 2011 00:17
Quote: "I suspect what's happening to Dar13 is that not enough stack space is being reserved for the UDT parameter within the function"

I was wrong.

It's that the calling code is only passing the first 4 bytes of each register to the stack before the function is called.

Now we know what's happening, but we don't know the circumstances - when I arrange GG's code in the same order as was used to generate the problem in the original code (global, assign, call function, define type, function), it compiles and runs cleanly



(Relevant sections from Dar13's dbm file - it's the lines starting with '68' that show what parameters are being pushed onto the stack)


tiresius
23
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 23rd Apr 2011 03:48
68 LINE : test,wtld_makeObjectCube(20,VECTOR_ZERO,VECTOR_ZERO,1)

What is "test," in that line ?


A 3D marble platformer using Newton physics.
Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 23rd Apr 2011 06:57
I've attached the .dbsource that CodeSurge generates when it compiles my project. Perhaps that would help?


Quote: "68 LINE : test,wtld_makeObjectCube(20,VECTOR_ZERO,VECTOR_ZERO,1)

What is "test," in that line ?"

It's the variable holding the generated object ID from the wtld_makeObjectCube() function.


Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Apr 2011 13:15 Edited at: 23rd Apr 2011 13:19
You're passing in "VECTOR_ZERO" but the global is called "ZERO_VEC"...

This seems to be uncovering some hidden functionality of DBPro. If you look in the DBM file to see what is being passed in it shows $E1 and $E2, these are defined as the following:

@$E1=40 [STRUCT@userdefined var ptr]
@$E2=44 [STRUCT@userdefined var ptr]

I've never seen a userdefined var ptr before

[b]
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Apr 2011 13:41
Quote: "Quote: "
Quote: "I believe globals can only be accessed by functions declared in the same file so perhaps something similar happens to constants."
That would be news to me.
"
Just wanted to clarify this one. "


Er, yes. I seem to have muddled myself on this one.

I thought I was following the advice given on the following thread but it seems my memory is playing tricks on me (or I failed to understand the advice first time round - choose whichever explanation you like - in fact both probably apply ):

declaring globals, etc, in functions and files
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Apr 2011 14:27
Quote: "You're passing in "VECTOR_ZERO" but the global is called "ZERO_VEC"..."

Sometimes we see what we expect to see because that's what is there. And sometimes we see what we expect to see because we expect it.

Well spotted.

Now I can reproduce the problem:


@Dar13, fix your function call and it'll work right. Make your function call with ZERO_VEC instead of VECTOR_ZERO.

The issue is that the function call is not passing a value of type vector3df (it's passing an integer), and is not raising an error for the incorrect type.

tiresius
23
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 23rd Apr 2011 15:10
Ha that is awesome.


A 3D marble platformer using Newton physics.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Apr 2011 19:25 Edited at: 23rd Apr 2011 19:27
It does let you do a C++ style reinterpret_cast though



[b]
Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 24th Apr 2011 07:20
@IanM, Diggsey, tiresius, Agent, GG
Thanks for all the help,it works fine now that I'm calling the function correctly. Glad we figured this thing out and also discovered an interesting quirk in DBPro.


Login to post a reply

Server time is: 2026-07-18 06:41:31
Your offset time is: 2026-07-18 06:41:31