Skip to content
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

fixed array getting wrong address #20378

Closed
enghitalo opened this issue Jan 3, 2024 · 3 comments
Closed

fixed array getting wrong address #20378

enghitalo opened this issue Jan 3, 2024 · 3 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@enghitalo
Copy link
Contributor

enghitalo commented Jan 3, 2024

Describe the bug

fixed array getting wrong address

Reproduction Steps

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)

	unsafe { str = &u8(&buffer2) }
	println(str)

	unsafe { str = &u8(&fixed_buffer) }
	println(str)

	unsafe { str = &u8(&fixed_buffer2) }
	println(str)
}

Expected Behavior

&<random>
&<random>
&<random>
&<random>

Current Behavior

&<random>
&<random>
&56
&56

Possible Solution

No response

Additional Information/Context

No response

V version

latest

Environment details (OS name and version, etc.)

Ubuntu

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.

@enghitalo enghitalo added the Bug This tag is applied to issues which reports bugs. label Jan 3, 2024
@shove70
Copy link
Contributor

shove70 commented Jan 4, 2024

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

@spytheman
Copy link
Member

@exapsy
Copy link

exapsy commented Jan 7, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

4 participants