Comments (3)
fn main() {
mut buffer := [u8(56), 55]
mut buffer2 := [u8(56), 55]
mut fixed_buffer := [u8(56), 55]!
mut fixed_buffer2 := [u8(56), 55]!
mut str := &u8(0)
unsafe { str = &u8(&buffer) }
println('${str: p}, buffer.data: ${buffer.data}')
unsafe { str = &u8(&buffer2) }
println('${str: p}, buffer2.data: ${buffer2.data}')
unsafe { str = &u8(&fixed_buffer) }
println('${str: p}, fixed_buffer[0]: ${&fixed_buffer[0]: p}')
unsafe { str = &u8(&fixed_buffer2) }
println('${str: p}, fixed_buffer2[0]: ${&fixed_buffer2[0]: p}')
}
The address is indeed random. the address of the array is actually the address of struct array, and the address of element [0] is buffer.data
Fixed arrays, have the same address as the element[0] because array is a struct, and fixed arrays are not
7ff7b2ed0908, buffer.data: 10d109ff0
7ff7b2ed08e0, buffer2.data: 10d109fe0
7ff7b2ed08dc, fixed_buffer[0]: 7ff7b2ed08dc
7ff7b2ed08da, fixed_buffer2[0]: 7ff7b2ed08da
from v.
After a discussion in the discord chat, long after the linked one, just for anybody who will fall onto this issue again, the conclusion is this:
println(&str)
essentially prints the first element.
Now, why is this done?
-
A Dynamic Array (slice/list or other names), in V is a struct of
type array struct { data voitptr, offset int, len int, cap int ... }
. Wheredata
is where the pointer of the actual array is stored. What this means, is that if you actually got the actual pointer of the array, you would get the pointer of thetype array
not of the actual array of elements. Which would reasonably be cumbersome, annoying, since you would have to know what thearray
struct is even and how to get access to the actual array's data. Not ideal at all. So, printing the first element ofarray
is just printingdata voidptr
, the first element, becausedata voidptr
is the first element ofarray
and it actually points to the actual array without you having to know what the internal V's struct of an array is like. -
A Fixed Array is just a
u8[len]
, as in a simple C like array. No structures or anything. So, printingprintln(&fixed_buffer)
just prints the first element of the array, because, in the case of a dynamic array you would actually get thedata voidptr
and it happens here that a fixed array is just au8[len]
.
As a user of the language but also someone who also likes to sympathize with the devs, I get both sides.
From user's perspective
I get it's annoying to think why they would both behave the same. They're both arrays in the eyes of the user. So, it's admittedly unexpected from a user to see these kind of results.
From the language's programmers' perspective
When one is a struct
(dynamic array), and the other is a u8 array
(fixed array), printing both's first value would of course produce the first value of both which is different, one is the pointer to the data, the other is the value of the first item in the data. The language would or will have to make a design around this issue to mitigate the fact that printing a fixed array's reference produces the first element
Ideally, from my personal perspective, both would have to behave the same. But the programming behind would or will have to be redesigned around that issue.
from v.
See also https://discord.com/channels/592103645835821068/592114487759470596/1192358296858923048
from v.
Related Issues (20)
- Builder C error: '_t1' undeclared
- Builder C error : identifier expected
- Incorrect f64 -> u64 conversions HOT 5
- compile-time reflection error for enum/struct declared in other module HOT 1
- fixed array: `contains` method HOT 1
- How to bypass https certificate check HOT 1
- Cannot get whole response body on concurrent requests when using -prod flag
- vlib: time.Duration and `-` operator leaves spurious ns reminder for a - b < 0 case. HOT 1
- UTF-8 Identifiers HOT 3
- Fixed Array Generic Function with Option/Result return type returns unexpected results
- builder error: Compiler confuses declarations of const expressions when they are located in different files
- StringReader using io module private constants
- nested expression is treaded as invalid when trying to call a method on it
- Error handling, compile error HOT 2
- checker: wrong type check inside `$for`
- vlib: os.execute and arguments with linefeeds causes invalid exit_code HOT 1
- ORM select from syntax error
- cgen erro: wrong sumtype cast function?
- Can Cosmopolitan/APE work for cross compiled bins
- About the passing of function arguments, references, and values in vlang. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from v.