Skip to content

Incorrect alignment of structs. #1248

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

Closed
BarabasGitHub opened this issue Jul 15, 2018 · 2 comments
Closed

Incorrect alignment of structs. #1248

BarabasGitHub opened this issue Jul 15, 2018 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@BarabasGitHub
Copy link
Contributor

I would think that these both need to produce a struct with an alignment of 8, instead TestStruct1 ends up with an alignment of 1.

const std = @import("std");

const TestStruct1 = struct {
    small: u8,
    big: u64,
};
const TestStruct2 = struct {
    big: u64,
    small: u8,
};

test "alignment" {
    std.debug.warn("\n");
    std.debug.warn("Align TestStruct1 = {}\n", u64(@alignOf(TestStruct1)));
    std.debug.warn("Align TestStruct2 = {}\n", u64(@alignOf(TestStruct2)));
    std.debug.assert(@alignOf(TestStruct1) == 8);
    std.debug.assert(@alignOf(TestStruct2) == 8);
}

In addition to this I think it would be helpful if you can specify the default alignment of structs. And maybe especially for packed structs. Currently all packed structs get an alignment of 1, while that might not be appropriate if you want to pass them to some C function.

@andrewrk
Copy link
Member

See #1052

For ABI compatibility with C, use extern struct.

@BarabasGitHub
Copy link
Contributor Author

Oops. I somehow missed the existing issue.
extern struct has the same issue by the way. TestStruct1 also gets an alignment of 1.

@andrewrk andrewrk added this to the 0.3.0 milestone Jul 16, 2018
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Jul 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants