-
Notifications
You must be signed in to change notification settings - Fork 18k
making a copy of struct and taking address of it returns pointer to the same struct #59341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
1 similar comment
|
as per example above:
and
it would be reasonable to assume that they should have the same outcome. but in case of first example if you modify how would you document a difference so new developers would understand it easily? |
|
I don't think that
we will actually get a real copy of |
From the spec:
Note that it does not say "... *x denotes a new variable of type T copied from the variable of type T pointed to by x". Among other reasons, this is needed so expressions like |
closing as working as intended. |
Thank you for pointing to spec. Appreciate it. |
What version of Go are you using (
go version
)?1.20.2
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I have a structure that I pass a pointer to function. Under some conditions I want to create a copy of the structure and get a pointer to it, because I want to modify a copy, not the structure itself. So doing something like this:
where "c" is a pointer to a structure.
Seems like "v" points to the same address as "c" and therefore when I modify fields in "v" - it actually modifies these fields in "c" as well (since pointer points to the same address). But if I split it in two different lines, such as
then now I can modify "v1" without affecting "c".
What did you expect to see?
Obviously if I make a copy of structure and take address of it, I would expect for pointer to point to a new structure, not to old one.
I guess some optimization happens when compiler sees
&*
but it really shouldn't, because developer might have a very clear intent to create a copy of a struct and get a pointer.The text was updated successfully, but these errors were encountered: