Skip to content

Array alignment #2673

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
daurnimator opened this issue Jun 13, 2019 · 4 comments
Closed

Array alignment #2673

daurnimator opened this issue Jun 13, 2019 · 4 comments

Comments

@daurnimator
Copy link
Contributor

As part of my module's public members, I want to expose a type that is an array of u8 with align(4).
This currently fails with

error: align qualifier invalid on array type
pub const State = [48]align(4)u8;
@andrewrk
Copy link
Member

The syntax is

pub const state align(4) = [48]u8{...}

Or

pub const state: [48]u8 align(4) = undefined

The alignment is a property of the variable (memory location), not the type.

@daurnimator
Copy link
Contributor Author

e.g. I have a function:

pub const State = [48]u8;
pub fn foo(state: *align(4)State) void {

And I want a user to be able to write:

fn bar() {
    var state: State = StateInitializer;
    foo(&state);
}

Then they get an alignment error.

How can they fix it?

@andrewrk
Copy link
Member

They would have to put the alignment on the state variable like this:

    var state: State align(4) = StateInitializer;

If you want to encapsulate this into a type you'll need to use a struct and this issue needs to be implemented: #1512

@daurnimator
Copy link
Contributor Author

If you want to encapsulate this into a type you'll need to use a struct and this issue needs to be implemented: #1512

Thanks. I'm working around it for now by lying about the type (saying it's a const State = [48 / 4]u32; instead) and using @sliceToBytes everywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants