Start with C++ for about a months then move entirely to C# and since the language I'm dealing with doesn't have pointer (Javascript, Python, PHP....) I really don't know the point of pointer. Now I'm learning Go because I want to program a distribute network. I searched for what is pointer and the answer doesn't seem to be beginner friendly. I can't really understand the point at all.
评论:
martiandreamer:
GTHell:Every variable you allocate, is stored at a particular memory address.
When you must pass around variables in your program, sometimes you want to be able to manipulate the contents directly. And sometimes the memory allocated is too big to pass around between functions.
So what a pointer allows you to do is, instead of pass around a literal value, you pass around it’s reference, which is always the same 32- or 64-bit size depending on the system’s architecture.
Likewise, when you need to pass a pointer to a function, you know the original address in memory of the variable inside the function, which enables you to overwrite its contents. Passing by literal value doesn’t let you do that.
A pointer is always “the address of ___”, eg if you make a pointer to a UInt32, it is always “the address of a UInt32”. If you make a pointer to a Byte[13], it is always “the address of a Byte[13]”.
Does that help?
martiandreamer:Yeah, to sum up pointer is just like a reference?
wittywitwitty:That’s exactly right. To use a real word analogy, imagine a trailer, a detached house and a condo building side-by-side on Main St.
The “pointers” are 1 Main St, 2 Main St and 3 Main St, but what’s actually referenced at those addresses can differ wildly.
GTHell:Kind of. My best attempt at an ELI5 explanation would be to say you could think of pointers sort of like associative array keys in php, dictionary keys in python, or object keys in javascript. Pointers let you access information in memory like keys let you access information in arrays and/or objects. Key's and pointers are different but the concept is the same.
wittywitwitty:hmm, I see the point but why are programming start to not expose the pointer to developer (C#) and then Golang come up with pointer. I know different language different purpose but why can't they keep thing the same? is there a reason behind that?
konart:I think having access to pointers depends on your needs. The concept behind pointers is indirection which is the ability to reference something. I've very rarely seen scenarios where pointer access was needed in interpreted languages like php and python. As far as C# is concerned it seems to be a design decision made by Microsoft. According to their docs "To maintain type safety and security, C# does not support pointer arithmetic, by default." Most of the time I think it comes down to what the language was intended for and designer preference.
wittywitwitty:C# does not support pointer arithmetic
Neither does Go
konart:My point still stands though, that's a design decisions. All languages are influenced by the opinions and needs of the author. It also depends how close to the metal a language needs be. Pointers can be dangerous so I don't think theres anything wrong with not supporting pointer arithmetic if it's not needed. Microsoft and Google both give basically the same reason for not supporting pointer arithmetic. Go, C#
konart:I get it. I just wanted to point out that Go and C# are the same in this regard.
konart:Levels of abstaction. Some languages try to make you life easier by doing the same thing under the hood.
Having pointers gives you more flexability, but also implies greater responsibility.
GTHell:a := 1
this will allocate some space in memory and put "1" there.
Now if you do something like
b := a
this will allocate some more memory and put "1" there.
Then you can do something like this:
c := &a
this wil put pointer to a into c. It will be pointing to the memory we allocated in step one.
Now if you change b to 2 (b = 2) your a will remain equal to 1. But if you change c to 2 (*c = 2) your a will change to 2 also as we were changing something that c was pointing to.
Very simple example:
package main import "fmt" func main() { a := 1 b := a c := &a b = 2 fmt.Println(a) fmt.Println(b) *c = 2 fmt.Println(a) fmt.Println(*c) }
This will print:
1
2
2
2
konart:Ahh, It's just a reference which can be apply to almost any thing even function?
lilfirelord:Try reading this https://golangbot.com/pointers/
GTHell:Every variable has an address in memory. Think of it like a street address. Street addresses make it possible to find places. Memory addresses make it possible to find variables. A pointer is a variable that holds the address of another variable.
// here we assign the variable n and it receives an address in memory so when we refer to n later, the computer knows how to find its value n := 42
// here we are assigning the memory address of n to the variable pn. pn := &n
// this is “dereferencing” the pointer. It basically says, find out the value of the memory address. It will print 42. fmt.Println(*pn)
// this will print the memory address itself fmt.Println(pn)
This example is trivial but the idea is still the same
Edit: to follow up. There are a couple reasons you’d want to use pointers. One is to save unnecessary copies. If you have a large struct and want to pass it to a function, it’s much cheaper to just pass the memory address of that struct rather than copying the entire thing. Or if you would want to “pass by reference” in which case when you pass variables to functions, they would be able to mutate the variable itself without need to copy it and then return it again.
lilfirelord:Yeah, I think I get the idea.
TheMerovius:See my edit above to see a couple reasons you might want to use pointers
GTHell:since the language I'm dealing with doesn't have pointer (Javascript, Python, PHP....) I really don't know the point of pointer.
The thing is, those languages do have pointers. They mostly don't have non-pointers, though, which is why they don't expose it to you. But whenever you have an object that modifies itself, you are using a pointer. If you pass a python-list to a function, which appends things to it, then that works, because you are actually passing a pointer. So, the real question is: What use are non-pointer?
And the answer to that is, that pointers are more expensive: If you need to follow a pointer, you might have to load new data into the cache, they need a bit more total space and you might need more instructions. Often, you don't need that cost, so it is advantageous to be able to decide yourself what data has the extra indirection and what data hasn't (this is called "controlling the memory layout"). Go allows you to do that, by distinguishing pointers from non-pointers.
ChristophBerger:By what you say can I assume that Go is a flexible language hence allow developer to have more control over what they doing that can result in performance boost?
paradoxops:Here is a visual explanation of pointers in Go. (Taken from my Go course.)
StupidRandomGuy:Dave Cheney wrote a good post on this. I think you will find it helpful.
Javascript uses pointer implicitly
