What pointer is for?

agolangf · · 441 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Start with C++ for about a months then move entirely to C# and since the language I&#39;m dealing with doesn&#39;t have pointer (Javascript, Python, PHP....) I really don&#39;t know the point of pointer. Now I&#39;m learning Go because I want to program a distribute network. I searched for what is pointer and the answer doesn&#39;t seem to be beginner friendly. I can&#39;t really understand the point at all.</p> <hr/>**评论:**<br/><br/>martiandreamer: <pre><p>Every variable you allocate, is stored at a particular memory address.</p> <p>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. </p> <p>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. </p> <p>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. </p> <p>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]”. </p> <p>Does that help?</p></pre>GTHell: <pre><p>Yeah, to sum up pointer is just like a reference? </p></pre>martiandreamer: <pre><p>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. </p> <p>The “pointers” are 1 Main St, 2 Main St and 3 Main St, but what’s actually referenced at those addresses can differ wildly. </p></pre>wittywitwitty: <pre><p>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&#39;s and pointers are different but the concept is the same.</p></pre>GTHell: <pre><p>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&#39;t they keep thing the same? is there a reason behind that?</p></pre>wittywitwitty: <pre><p>I think having access to pointers depends on your needs. The concept behind pointers is indirection which is the ability to reference something. I&#39;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 &#34;To maintain type safety and security, C# does not support pointer arithmetic, by default.&#34; Most of the time I think it comes down to what the language was intended for and designer preference.</p></pre>konart: <pre><blockquote> <p>C# does not support pointer arithmetic</p> </blockquote> <p>Neither does Go</p></pre>wittywitwitty: <pre><p>My point still stands though, that&#39;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&#39;t think theres anything wrong with not supporting pointer arithmetic if it&#39;s not needed. Microsoft and Google both give basically the same reason for not supporting pointer arithmetic. <a href="https://golang.org/doc/faq#no_pointer_arithmetic" rel="nofollow">Go</a>, <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/unsafe-code-pointers/" rel="nofollow">C#</a></p></pre>konart: <pre><p>I get it. I just wanted to point out that Go and C# are the same in this regard.</p></pre>konart: <pre><p>Levels of abstaction. Some languages try to make you life easier by doing the same thing under the hood.</p> <p>Having pointers gives you more flexability, but also implies greater responsibility.</p></pre>konart: <pre><p>a := 1</p> <p>this will allocate some space in memory and put &#34;1&#34; there.</p> <p>Now if you do something like </p> <p>b := a</p> <p>this will allocate some more memory and put &#34;1&#34; there.</p> <p>Then you can do something like this:</p> <p>c := &amp;a</p> <p>this wil put pointer to a into c. It will be pointing to the memory we allocated in step one.</p> <p>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.</p> <p>Very simple example:</p> <pre><code>package main import &#34;fmt&#34; func main() { a := 1 b := a c := &amp;a b = 2 fmt.Println(a) fmt.Println(b) *c = 2 fmt.Println(a) fmt.Println(*c) } </code></pre> <p>This will print:</p> <p>1</p> <p>2</p> <p>2</p> <p>2</p></pre>GTHell: <pre><p>Ahh, It&#39;s just a reference which can be apply to almost any thing even function?</p></pre>konart: <pre><p>Try reading this <a href="https://golangbot.com/pointers/" rel="nofollow">https://golangbot.com/pointers/</a></p></pre>lilfirelord: <pre><p>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. </p> <p>// 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</p> <p>// here we are assigning the memory address of n to the variable pn. pn := &amp;n</p> <p>// this is “dereferencing” the pointer. It basically says, find out the value of the memory address. It will print 42. fmt.Println(*pn)</p> <p>// this will print the memory address itself fmt.Println(pn)</p> <p>This example is trivial but the idea is still the same</p> <p>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. </p></pre>GTHell: <pre><p>Yeah, I think I get the idea. </p></pre>lilfirelord: <pre><p>See my edit above to see a couple reasons you might want to use pointers</p></pre>TheMerovius: <pre><blockquote> <p>since the language I&#39;m dealing with doesn&#39;t have pointer (Javascript, Python, PHP....) I really don&#39;t know the point of pointer.</p> </blockquote> <p>The thing is, those languages <em>do</em> have pointers. They mostly don&#39;t have non-pointers, though, which is why they don&#39;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 <em>non-pointer</em>?</p> <p>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&#39;t need that cost, so it is advantageous to be able to decide yourself what data has the extra indirection and what data hasn&#39;t (this is called &#34;controlling the memory layout&#34;). Go allows you to do that, by distinguishing pointers from non-pointers.</p></pre>GTHell: <pre><p>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? </p></pre>ChristophBerger: <pre><p>Here is a <a href="https://appliedgo.com/courses/mastergo/lectures/2618279" rel="nofollow">visual explanation</a> of pointers in Go. (Taken from my Go course.)</p></pre>paradoxops: <pre><p>Dave Cheney wrote a good <a href="https://dave.cheney.net/2017/04/26/understand-go-pointers-in-less-than-800-words-or-your-money-back" rel="nofollow">post</a> on this. I think you will find it helpful. </p></pre>StupidRandomGuy: <pre><p>Javascript uses pointer implicitly</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

441 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传