Creating a variable creates a copy, dereferencing does not. Why?What are the differences between a pointer variable and a reference variable in C++?With arrays, why is it the case that a[5] == 5[a]?What does “dereferencing” a pointer mean?Why does the arrow (->) operator in C exist?Why should I use a pointer rather than the object itself?How does pointer dereferencing work in golang?Is it safe to use the memory address of descoped variables?If you malloc a struct* does it create local variables?Is it possible to initialize a C pointer to NULL?Why is the golang range operator implemented with declaration of a local variable?

Can living where Rare Earth magnetic ore is abundant provide any protection from cosmic radiation?

How do I safety check that there is no light in Darkroom / Darkbag?

May a hotel provide accommodation for fewer people than booked?

Why didn't General Martok receive discommendation in Star Trek: Deep Space Nine?

Patio gate not at right angle to the house

How to structure presentation to avoid getting questions that will be answered later in the presentation?

UX writing: When to use "we"?

Should I put my name first or last in the team members list?

Why don't short runways use ramps for takeoff?

Password management for kids - what's a good way to start?

Music Theory: Facts or Hierarchy of Opinions?

What is the term for completing a climbing route uncleanly?

How can a class have multiple methods without breaking the single responsibility principle

How would a lunar colony attack Earth?

What is my clock telling me to do?

Just how much information should you share with a former client?

Should students have access to past exams or an exam bank?

Should 2FA be enabled on service accounts?

"Fewer errors means better products" or fewer errors mean better products."

Avoiding Implicit Conversion in Constructor. Explicit keyword doesn't help here

What is the oxidation state of Mn in HMn(CO)5?

Numerically Stable IIR filter

Prepare a user to perform an action before proceeding to the next step

Coworker mumbles to herself when working, how to ask her to stop?



Creating a variable creates a copy, dereferencing does not. Why?


What are the differences between a pointer variable and a reference variable in C++?With arrays, why is it the case that a[5] == 5[a]?What does “dereferencing” a pointer mean?Why does the arrow (->) operator in C exist?Why should I use a pointer rather than the object itself?How does pointer dereferencing work in golang?Is it safe to use the memory address of descoped variables?If you malloc a struct* does it create local variables?Is it possible to initialize a C pointer to NULL?Why is the golang range operator implemented with declaration of a local variable?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-2















Why does putting a value into a variable create a copy but de-referencing does not?



Is it a simple optimization of the compiler that knows that it can just use the address of the original structure, while creating a variable always allocates new memory?



Example 1:



x1 := &struct x int x: 0
y1 := *x1
z1 := &y1
z1.x++

fmt.Printf("--- 1:n%#vn%#vn", x1, z1)


Example 2:



x2 := &struct x int x: 0
z2 := &*x2
z2.x++

fmt.Printf("--- 2:n%#vn%#vn", x2, z2)


Run here: https://play.golang.org/p/myugNmjrQFj



Is there a a part of the go documentation that describes this behavior?










share|improve this question



















  • 4





    Neither creating a variable, nor dereferencing create copies. Copy happens on assignment. = &*x2 copies (assigns) the pointer value to your new variable.

    – JimB
    Mar 26 at 22:47












  • So basically, by creating a variable I allocate memory and assigning then fills that memory by copying the data. In my second example the data is never copied because there is no memory allocated where the data could go in between de-referencing and getting the pointer. Did I understand that correctly?

    – sirion
    Mar 26 at 22:54






  • 2





    I guess you could put it that way, but I don't think you need to complicate it so. The simple concept "assignment == copy" works for everything. The odd expression &*x2 doesn't assign anything, therefor doesn't copy anything.

    – JimB
    Mar 26 at 22:59


















-2















Why does putting a value into a variable create a copy but de-referencing does not?



Is it a simple optimization of the compiler that knows that it can just use the address of the original structure, while creating a variable always allocates new memory?



Example 1:



x1 := &struct x int x: 0
y1 := *x1
z1 := &y1
z1.x++

fmt.Printf("--- 1:n%#vn%#vn", x1, z1)


Example 2:



x2 := &struct x int x: 0
z2 := &*x2
z2.x++

fmt.Printf("--- 2:n%#vn%#vn", x2, z2)


Run here: https://play.golang.org/p/myugNmjrQFj



Is there a a part of the go documentation that describes this behavior?










share|improve this question



















  • 4





    Neither creating a variable, nor dereferencing create copies. Copy happens on assignment. = &*x2 copies (assigns) the pointer value to your new variable.

    – JimB
    Mar 26 at 22:47












  • So basically, by creating a variable I allocate memory and assigning then fills that memory by copying the data. In my second example the data is never copied because there is no memory allocated where the data could go in between de-referencing and getting the pointer. Did I understand that correctly?

    – sirion
    Mar 26 at 22:54






  • 2





    I guess you could put it that way, but I don't think you need to complicate it so. The simple concept "assignment == copy" works for everything. The odd expression &*x2 doesn't assign anything, therefor doesn't copy anything.

    – JimB
    Mar 26 at 22:59














-2












-2








-2








Why does putting a value into a variable create a copy but de-referencing does not?



Is it a simple optimization of the compiler that knows that it can just use the address of the original structure, while creating a variable always allocates new memory?



Example 1:



x1 := &struct x int x: 0
y1 := *x1
z1 := &y1
z1.x++

fmt.Printf("--- 1:n%#vn%#vn", x1, z1)


Example 2:



x2 := &struct x int x: 0
z2 := &*x2
z2.x++

fmt.Printf("--- 2:n%#vn%#vn", x2, z2)


Run here: https://play.golang.org/p/myugNmjrQFj



Is there a a part of the go documentation that describes this behavior?










share|improve this question














Why does putting a value into a variable create a copy but de-referencing does not?



Is it a simple optimization of the compiler that knows that it can just use the address of the original structure, while creating a variable always allocates new memory?



Example 1:



x1 := &struct x int x: 0
y1 := *x1
z1 := &y1
z1.x++

fmt.Printf("--- 1:n%#vn%#vn", x1, z1)


Example 2:



x2 := &struct x int x: 0
z2 := &*x2
z2.x++

fmt.Printf("--- 2:n%#vn%#vn", x2, z2)


Run here: https://play.golang.org/p/myugNmjrQFj



Is there a a part of the go documentation that describes this behavior?







pointers go






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 22:41









sirionsirion

9335 silver badges12 bronze badges




9335 silver badges12 bronze badges










  • 4





    Neither creating a variable, nor dereferencing create copies. Copy happens on assignment. = &*x2 copies (assigns) the pointer value to your new variable.

    – JimB
    Mar 26 at 22:47












  • So basically, by creating a variable I allocate memory and assigning then fills that memory by copying the data. In my second example the data is never copied because there is no memory allocated where the data could go in between de-referencing and getting the pointer. Did I understand that correctly?

    – sirion
    Mar 26 at 22:54






  • 2





    I guess you could put it that way, but I don't think you need to complicate it so. The simple concept "assignment == copy" works for everything. The odd expression &*x2 doesn't assign anything, therefor doesn't copy anything.

    – JimB
    Mar 26 at 22:59













  • 4





    Neither creating a variable, nor dereferencing create copies. Copy happens on assignment. = &*x2 copies (assigns) the pointer value to your new variable.

    – JimB
    Mar 26 at 22:47












  • So basically, by creating a variable I allocate memory and assigning then fills that memory by copying the data. In my second example the data is never copied because there is no memory allocated where the data could go in between de-referencing and getting the pointer. Did I understand that correctly?

    – sirion
    Mar 26 at 22:54






  • 2





    I guess you could put it that way, but I don't think you need to complicate it so. The simple concept "assignment == copy" works for everything. The odd expression &*x2 doesn't assign anything, therefor doesn't copy anything.

    – JimB
    Mar 26 at 22:59








4




4





Neither creating a variable, nor dereferencing create copies. Copy happens on assignment. = &*x2 copies (assigns) the pointer value to your new variable.

– JimB
Mar 26 at 22:47






Neither creating a variable, nor dereferencing create copies. Copy happens on assignment. = &*x2 copies (assigns) the pointer value to your new variable.

– JimB
Mar 26 at 22:47














So basically, by creating a variable I allocate memory and assigning then fills that memory by copying the data. In my second example the data is never copied because there is no memory allocated where the data could go in between de-referencing and getting the pointer. Did I understand that correctly?

– sirion
Mar 26 at 22:54





So basically, by creating a variable I allocate memory and assigning then fills that memory by copying the data. In my second example the data is never copied because there is no memory allocated where the data could go in between de-referencing and getting the pointer. Did I understand that correctly?

– sirion
Mar 26 at 22:54




2




2





I guess you could put it that way, but I don't think you need to complicate it so. The simple concept "assignment == copy" works for everything. The odd expression &*x2 doesn't assign anything, therefor doesn't copy anything.

– JimB
Mar 26 at 22:59






I guess you could put it that way, but I don't think you need to complicate it so. The simple concept "assignment == copy" works for everything. The odd expression &*x2 doesn't assign anything, therefor doesn't copy anything.

– JimB
Mar 26 at 22:59













1 Answer
1






active

oldest

votes


















0















Is there a a part of the go documentation that describes this behavior?




Yes, the language spec. See https://golang.org/ref/spec






share|improve this answer

























  • Gesturing vaguely at a 100-page document covering every aspect of the language doesn't answer the question. Directing them to the relevant headings would offer more value to the asker and to future developers landing on this question from searches. Also quoting the relevant sections would make this an actual answer; a link with no details does not.

    – Adrian
    Mar 27 at 14:09











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367210%2fcreating-a-variable-creates-a-copy-dereferencing-does-not-why%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















Is there a a part of the go documentation that describes this behavior?




Yes, the language spec. See https://golang.org/ref/spec






share|improve this answer

























  • Gesturing vaguely at a 100-page document covering every aspect of the language doesn't answer the question. Directing them to the relevant headings would offer more value to the asker and to future developers landing on this question from searches. Also quoting the relevant sections would make this an actual answer; a link with no details does not.

    – Adrian
    Mar 27 at 14:09
















0















Is there a a part of the go documentation that describes this behavior?




Yes, the language spec. See https://golang.org/ref/spec






share|improve this answer

























  • Gesturing vaguely at a 100-page document covering every aspect of the language doesn't answer the question. Directing them to the relevant headings would offer more value to the asker and to future developers landing on this question from searches. Also quoting the relevant sections would make this an actual answer; a link with no details does not.

    – Adrian
    Mar 27 at 14:09














0












0








0








Is there a a part of the go documentation that describes this behavior?




Yes, the language spec. See https://golang.org/ref/spec






share|improve this answer














Is there a a part of the go documentation that describes this behavior?




Yes, the language spec. See https://golang.org/ref/spec







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 4:28









VolkerVolker

22.5k3 gold badges52 silver badges60 bronze badges




22.5k3 gold badges52 silver badges60 bronze badges















  • Gesturing vaguely at a 100-page document covering every aspect of the language doesn't answer the question. Directing them to the relevant headings would offer more value to the asker and to future developers landing on this question from searches. Also quoting the relevant sections would make this an actual answer; a link with no details does not.

    – Adrian
    Mar 27 at 14:09


















  • Gesturing vaguely at a 100-page document covering every aspect of the language doesn't answer the question. Directing them to the relevant headings would offer more value to the asker and to future developers landing on this question from searches. Also quoting the relevant sections would make this an actual answer; a link with no details does not.

    – Adrian
    Mar 27 at 14:09

















Gesturing vaguely at a 100-page document covering every aspect of the language doesn't answer the question. Directing them to the relevant headings would offer more value to the asker and to future developers landing on this question from searches. Also quoting the relevant sections would make this an actual answer; a link with no details does not.

– Adrian
Mar 27 at 14:09






Gesturing vaguely at a 100-page document covering every aspect of the language doesn't answer the question. Directing them to the relevant headings would offer more value to the asker and to future developers landing on this question from searches. Also quoting the relevant sections would make this an actual answer; a link with no details does not.

– Adrian
Mar 27 at 14:09









Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367210%2fcreating-a-variable-creates-a-copy-dereferencing-does-not-why%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript