What is wrong with my code

How model_name is getting modified, even though it prints expected result.

// ***** Get CPU Statistics *****
//     - Number of Cores: 4
//     - CPU Model: Intel Core i7
//     - CPU Usage: 25%
//

const std = @import("std");

fn cpuinfo() !void {
    var cores: u8 = 0;
    var model_name: []const u8 = undefined;

    var file_buf = [_]u8{0} ** 1024;

    const file = try std.fs.cwd().openFile("/proc/cpuinfo", .{});
    defer file.close();

    var reader = file.reader();

    while (try reader.readUntilDelimiterOrEof(&file_buf, '\n')) |line| {
        var splitted = std.mem.split(u8, line, ":");

        const first = splitted.first()[0..];

        if (std.mem.eql(u8, first, "cpu cores\t")) {
            cores += 1;
        }

        if (std.mem.eql(u8, first, "model name\t")) {
            const model = std.mem.trim(u8, splitted.next().?, " ");
            std.debug.print("Model: {s}\n", .{model});
            model_name = model[0..];
        }
    }

    std.debug.print("\nCores : {}\nModel : {s}\n", .{ cores, model_name });
}

pub fn main() !void {
    try cpuinfo();
}
Model: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
Model: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
Model: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
Model: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
Model: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
Model: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
Model: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
Model: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx

Cores : 8
Model : ent: ts ttp tm hwpstate eff_freq_ro [13] [14]

Posted Aug 15, 2024, 7:43 AM UTC

21
Loading comments…