~sircmpwn/hare-users

2 2

insert in slices

Details
Message ID
<168488574708.1593.18119452817308360051@localhost>
DKIM signature
missing
Download raw message
Why do the following programs give different outputs?

```
use fmt;

export fn main() void = {
	let s: []uint = [1];
	insert(s[0], 0);
	fmt::println(s[0])!;
	fmt::println(s[1])!;
};
```

Output:
0
2374983655

```
use fmt;

export fn main() void = {
	let s: []uint = [1];
	fmt::print()!;
	insert(s[0], 0);
	fmt::println(s[0])!;
	fmt::println(s[1])!;
};
```

Output:
0
1

(Version: Hare dev+9a62f94f)
Details
Message ID
<CSU2EO9WJYLQ.OYF3QQAS0B94@monch>
In-Reply-To
<168488574708.1593.18119452817308360051@localhost> (view parent)
DKIM signature
missing
Download raw message
On Tue May 23, 2023 at 11:49 PM UTC, Tilman List wrote:
> export fn main() void = {
> 	let s: []uint = [1];
> 	insert(s[0], 0);

this code is incorrect - insert does the equivalent of a realloc() on
the slice. malloc currently doesn't detect the invalid free, so you'll
end up with undefined behavior instead, but in the future this code will
just crash properly

> 	fmt::println(s[0])!;
> 	fmt::println(s[1])!;
> };
Details
Message ID
<168499025195.3078.5710348734520571652@localhost>
In-Reply-To
<CSU2EO9WJYLQ.OYF3QQAS0B94@monch> (view parent)
DKIM signature
missing
Download raw message
Thank you!
Reply to thread Export thread (mbox)