Coder Social home page Coder Social logo

fixed array getting wrong address about v HOT 3 CLOSED

enghitalo avatar enghitalo commented on May 23, 2024 2
fixed array getting wrong address

from v.

Comments (3)

shove70 avatar shove70 commented on May 23, 2024 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.

exapsy avatar exapsy commented on May 23, 2024 1

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 ... }. Where data 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 the type array not of the actual array of elements. Which would reasonably be cumbersome, annoying, since you would have to know what the array struct is even and how to get access to the actual array's data. Not ideal at all. So, printing the first element of array is just printing data voidptr, the first element, because data voidptr is the first element of array 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, printing println(&fixed_buffer) just prints the first element of the array, because, in the case of a dynamic array you would actually get the data voidptr and it happens here that a fixed array is just a u8[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.

spytheman avatar spytheman commented on May 23, 2024

See also https://discord.com/channels/592103645835821068/592114487759470596/1192358296858923048

from v.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.