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

changing pointers #1688

Closed
emekoi opened this issue Oct 28, 2018 · 4 comments
Closed

changing pointers #1688

emekoi opened this issue Oct 28, 2018 · 4 comments

Comments

@emekoi
Copy link
Contributor

emekoi commented Oct 28, 2018

in my code i pass a pointer to a struct to one of it's method but the pointer's address differs from the address of the actual struct by 20. the address of the actual struct also differs from the address of the struct i return in the initializer by 4 as well. the code in question is here. running the code on windows will produce 3 different values for the address of the window. an anyone tell me why the pointers are all different?

@winksaville
Copy link
Contributor

winksaville commented Oct 28, 2018 via email

@emekoi
Copy link
Contributor Author

emekoi commented Oct 28, 2018

here's an example that's just produces different random pointer values:

const std = @import("std");

pub fn main() void {
    const opts = Window.Options.{ .title = "hello_world" };
    var window = Window.init(opts);
    std.debug.warn("main: {}\n", @ptrToInt(&window));
    window.info();
}

pub const Window = struct.{
    const Self = @This();

    pub const Options = struct.{ title: []const u8 };

    title: []const u8,

    pub fn init(options: Options) Self {
        var result = Self.{ .title = options.title };
        std.debug.warn("init: {}\n", @ptrToInt(&result));
        return result;
    }

    pub fn info(self: *Self) void {
        std.debug.warn("info: {}\n", @ptrToInt(&self));
    }

};

@tgschultz
Copy link
Contributor

tgschultz commented Oct 28, 2018

    pub fn init(options: Options) Self {
        var result = Self.{ .title = options.title };
        std.debug.warn("init: {}\n", @ptrToInt(&result));
        return result;
    }

copy elision isn't in yet (see #287, #1682), so the result created in this function body is copied on return.

    pub fn info(self: *Self) void {
        std.debug.warn("info: {}\n", @ptrToInt(&self));
    }

self is a pointer, so &self is a pointer to a pointer. @ptrToInt(self) should be correct (that is, it should match main:).

pub fn main() void {
    const opts = Window.Options.{ .title = "hello_world" };
    var window = Window.init(opts);
    std.debug.warn("main: {}\n", @ptrToInt(&window));
    window.info();
}

window is the instance that was copied from result created in Window.init.

@emekoi
Copy link
Contributor Author

emekoi commented Oct 28, 2018

thanks! this was really messing me up.

@emekoi emekoi closed this as completed Oct 28, 2018
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

3 participants