-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
&string interpolation with += string #23460
Comments
Connected to Huly®: V_0.6-21890 |
What did you expect to happen with your example? Looks you're trying to add a string to a string pointer, which is exactly what V said. |
This works even without fn main() {
s := "Hello"
p := &s
unsafe { *p += s }
println('${*p}') // prints HelloHello
} |
Exponential grow without fn main() {
s := "Hello"
p := &s
for i := 0; i < 4; i++ {
unsafe { *p += *p }
}
println('${s}') // Hello 2^4 = 16 times
} |
I guess I did not understand properly what V meant by "pointer arithmetic". Also it says that += is not defined on type string, instead of &string |
No, And adding a string to the string pointed to by a pointer will just overwrite unallocated memory (or worse, allocated for something else). Both of @jorgeluismireles options may "work", but they have corrupted memory beyond what was correctly allocated for The string struct also has not been properly updated, so So, although it is certainly possible to make this kind of thing work, it is definitely |
V pointer arithmetic message seems to be like the C pointer arithmetic, but seems is not. In C, with pointer arithmetic you can read and write each byte of an integer, struct, string, array... having the start of the object. But you are in your own because you can modify parts of other objects adjacent set by the compiler and create unexpected bugs or create viruses or security threats. In V seems to be just the possibility of use the |
Okay thank you! |
V version: V 0.4.9 a20fff9, press to see full `v doctor` output
What did you do?
./v -g -o vdbg cmd/v && ./vdbg bug.v && bug
What did you see?
What did you expect to see?
To say it is not defined on &string (because it is defined for string). But I dont know if it should allow for &string += string .
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.
The text was updated successfully, but these errors were encountered: