Pointers

BlitzMax Forums/BlitzMax Beginners Area/Pointers

Yue(Posted 2015) [#1]
I see BlitzMax accepts C-style pointers and not if I failed to understand the whole concept. which I set out below and are free to correct me.

"The pointers point to the physical address of a variable", at this point everything looks very simple, so I say 5 sectors of memory.

| 1 || 2 | | 3 | | 4 | | 5 |

When creating a random variable physical address of the variable it is set.

Local number:Int = 200

   
| 1 |
| 2 | 
|200 | number
| 4 | 
| 5 |



Now, using a pointer what I understand is that I return the memory location where this variable, in this case the position of memory 3.

If I'm right, what it is to know the physical location of a variable with a pointer?

How can this be practical programming, I can do with that memory?


grable(Posted 2015) [#2]
Essentially EVERYTHING you do in programming relates to pointers in some way or another.

The automatic variables you get from locals and function arguments are the simplest form of this.
They are allocated on "The Stack", which is also a pointer that grows or shrinks as needed.

Taking the address of automatic variables isnt all that useful really, at least not when you only have immediate types to work with.
And there is the problem of it going out of scope when leaving a function and all that (hence the name "automatic" variables)

And BlitzMax has Var arguments so the need for taking their address is even lower than for C.

The real fun with pointers though, start when you allocate a chunk of memory and manage it yourself.
Think of it as an un-typed object, all you have is its address and can from there read and write whatever you want to it
(so long as you dont exceed its actual size that is, or bad things will happen)

So if you are unclear of what you can use pointers for.. Dont use them.
Their use will become clearer when you are a more proficient programmer.

Just know that you are always using pointers under the hood. Each object is a block of memory, and every field access is a pointer dereference off that address.
And the same is true for globals (and automatic variables, barring any optimizations the compiler might do)


Yasha(Posted 2015) [#3]
Pointers are useful, but they are mainly included in BlitzMax for interfacing with C and assembly, not really for writing new code. Most languages hide pointers from you, and provide easier alternatives for you to work with, like objects.

1) You don't need to "know" pointers to work in BlitzMax or most similar languages.

2) Pointers in BlitzMax are quite clumsy - if you do want to learn them, you'd probably find it easier to learn C, where they're a major language feature. Most of the people who use pointers in BlitzMax are the users who already knew C.

Grable is right, pointers are one of the most fundamental concepts in programming - but you probably don't want to get your knowledge of them from BlitzMax.


Floyd(Posted 2015) [#4]
It's not very common but sometimes manipulating pointers is the only option.

A while ago there was a discussion about floating point accuracy. What is the best way to determine if two values are "almost equal"?

Here is one possibility: http://www.blitzbasic.com/Community/post.php?topic=104771&post=1271802


ziggy(Posted 2015) [#5]
IMHO, on managed languages like BlitzMax, pointers should be avoided in favor of References, if possible. And it's possible 99.9% of the time.