-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Segmentation fault referencing values in anonymous tuple. #7878
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
Comments
The crash has nothing to do with const std = @import("std");
pub fn main() !void {
const Header = struct {
value: []const u8,
};
const allocator = std.heap.page_allocator;
var value = try allocator.dupe(u8, "test");
defer allocator.free(value);
var hdr = Header{ .value = value };
std.debug.print("{s}\n", .{hdr111});
} |
I've noticed this is LLVM behavior. I was implementing print function in my own language and generating LLVM IR, I've noticed that passing anonymous "values" (I don't have a better word) causes segfaults. I was passing in a string without first declaring it, hence the segfault. I then declared the string separately and then passed its pointer in it, and it worked. I've looked at LLVM IR generated by clang and that's how it does it too. I feel like programmers shouldn't have to worry about this internal implementation detail, but I guess this method also forces the programmer to write better code, i.e. easier to read (when it gets bigger and complicated), easier to catch errors, etc. |
repro.zigconst std = @import("std");
pub fn main() !void {
const Header = struct {
value: []const u8,
};
const allocator = std.heap.page_allocator;
var value = try allocator.dupe(u8, "test");
defer allocator.free(value);
std.debug.print("{}\n", .{Header{ .value = value }});
} $ archlinux▸ uname -a
Linux archlinux 6.1.2-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 31 Dec 2022 17:40:35 +0000 x86_64 GNU/Linux
$ zig version
0.11.0-dev.1239+7a2d7ff62
$ zig run repro.zig
repro.main.Header{ .value = { 116, 101, 115, 116 } } |
Uh oh!
There was an error while loading. Please reload this page.
If I un-encapsulate the heap-allocated string out of a struct, the test case passes. EDIT: as @LemonBoy has mentioned, the problem appears to be with printing out structs declared inside an anonymous tuple.
The text was updated successfully, but these errors were encountered: