forums

TNT Basic Forums > Feedback
Pointers
< Last Thread     Next Thread >
Author
Thread        Post A Reply

03-19-2003 21:32

Posted by:
Wil Hostman

Find more posts by Wil Hostman

I just ran across a "Code optimization" issue, where I wish I'd had pointers.

eg:
<code>
int aa, bb, cc, dd, num
if aa > 1
aa = aa +1
num = num -1
end if
if bb > 1
bb = bb + 1
num = num -1
end if
if cc > 1
cc = cc + 1
num = num -1
end if
if dd > 1
dd = dd + 1
num = num -1
end if
</code>
could all be replaced with
<code>
int aa, bb, cc, dd,
global int num
doNum(&aa)
doNum(&bb)
doNum(&cc)
doNum(&dd)

procedure donum(intptr x)
if x > 1
x = x + 1
num = num -1
enf if
end proc
</code>

while the example saves about two lines, if the same thing is used repeatedly, it shortens the code drastically, and leavs it more readable, too!

I'n not in dire need of "Direct Memory Access" pointers, just being able to have a proc that allows me to work on passed VARIABLES, rather than just their values.

BTW, thanks for adding the text file support. (I'm making use of it!)

03-19-2003 22:25

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

We'll add it to our todo list!

Thanks for the feedback,

Mark

04-08-2003 06:46

Posted by:
Holmes

Location:
Santa Rosa, Ca

Click Here to Email Holmes   Find more posts by Holmes

*shudder* pointers....yucky :P

04-09-2003 22:33

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

Sorry, we wouldn't actually do pointers but do variables that you could pass to a procedure or function that could have values returned in it - see the thread on returning values:

http://www.tntbasic.com/community/forums/showthread.php?threadid=555

Mark

04-15-2003 21:08

Posted by:
Wil Hostman

Find more posts by Wil Hostman

Pointers as Variables Conatining Variables

In programming in C and in BASIC, I've encounted the following uses for pointers:
1) direct access to memory (Ickky stuff)
2) passing arrays as a single parameter (sloppy)
3) receiving a variable to be worked upon rather than passing just the value contained in the variable.
4) C string manipulations

of these, only #3 is terribly useful in TNTBasic: code optimization being the best reason, but not the only one.

Passing scope is a second reason. this means easier code "chunking"; if I have a repeated output, I can chunk it, but still not need to make the shared variables global, and can pass multiple back.

I've seen at least one other basic that allowed passing a variable to a pointer. The C/C++ *x and &x. are, at least to me, somewhat confusing, but being able to set a type to pointers would be good, if solely for passing variables rather than their contents.

08-04-2003 08:14

Posted by:
Holmes

Location:
Santa Rosa, Ca

Click Here to Email Holmes   Find more posts by Holmes

I'm not sure if pointers have a place in BASIC. I mean, dealing with them in C can be hard enough from time to time. I'd rather not start getting SIGBUS and SIGSEV and SIGOMFGYOURCOMPUTERDIED from my TNT apps. How much speed can you really save by passing a variable address than passing the variable and setting it equal to the return value? Most time in a TNT app is drawing, I would imagine. I think the latter is a safer, better solution. I never really liked making thinks equal to result int or result float either...

08-04-2003 10:06

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

I agree. I use basic because it doesn't have all of that complicated stuff like pointers. If you don't want to make the variables global, you could use shared variables as an alternative.

08-04-2003 14:18

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

TNT Basic is... basic. I don't think it should have functions found in O.O. programming. When I want O.O. programming, I use Java. ( It's slow but it's cross-platform and it's free. ) I use TNT to have BASIC and relax with simple coding. I think there are much more important things to develop in TNT.

08-04-2003 18:42

Posted by:
Holmes

Location:
Santa Rosa, Ca

Click Here to Email Holmes   Find more posts by Holmes

Pointers aren't just for O.O.P, though thats a nice use for them.

08-16-2003 06:09

Posted by:
Wil Hostman

Find more posts by Wil Hostman

Passing Variables themselves...

The kind of "pointerish" behavior I want to see is already in several basic flavors.

Qbasic, specifically, supports passing variables using a byref keyword, in order to act on the original variable rather than the local one.

As I said, it is useful for the above reasons. Mostly code optimization and acting on the variable passed rather than simply using a value passed.

08-16-2003 14:51

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I agree, but the user should have the choice the use them or not to use them. Beginners can get really confused with pointers and TNT is perfect for beginners.

08-21-2003 03:30

Posted by:
Wil Hostman

Find more posts by Wil Hostman

I think you missed the pointer...

Matthew:

I have NEVER been suggesting mandatory useage of C-style pointers. (While I can use them, they are often hard to utilize).

The ability to pass a variable and act upon its contents directly, rather than passing the value and having that value acted upon is a major benefit. One which can be ignored safely for beginners, or for simple programs. But, in an interpreted language, the code optimization value alone is probably worthwhile... reduced code length is faster optomization and executions.

That several flavors of basic use the Byref token to indicate a variable is being passed as a container not a value is also important; it is NOTHING NEW to the Basic lexicon.

it opens up whole new realms of coding accuracy, too... far less cut and paste.

The way byref works:
Sub Foo(Int Byref A)
a = a +5
writeline(str$(a),100,100) //prolly got syntax way wrong here...
end sub

z = 10
Foo(z)

outputs 15, and z winds up witha a value of 15.

if the byref weren't there, the print would be the same, bt z would still be 10.

both modes have their uses.

08-21-2003 14:59

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Pointers ARE great! I just thought the "todo list" mnust be full enough without adding something we can work around. But if they do make pointers in TNT I will surely use them.

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.