so here is the code block:
const std = @import("std");
pub fn main() !void {
const allocator = std.heap.page_allocator;
const x = try allocator.alloc(u8, 10);
defer allocator.free(x);
std.debug.print("Type of x: {}\n", .{@TypeOf(x)});
const y: [*]u8 = x.ptr;
std.debug.print("Type of x: {}\n", .{@TypeOf(y)});
for(0..10) |i|{
std.debug.print("Type of {d}: {}\n", .{y[i], @TypeOf(y[i])});
}
}
my intent behind this was to just see what types are being passes around and see how pointers work in zig.
when i ran this using zig run file.zig, i got this:
Type of x: []u8
Type of x: [*]u8
Type of 170: u8
Type of 170: u8
Type of 170: u8
Type of 170: u8
Type of 170: u8
Type of 170: u8
Type of 170: u8
Type of 170: u8
Type of 170: u8
Type of 170: u8
- first i though the os must have done this then i learnt that modern os zero out the memory before handing it over when a process asks for it via syscall.
- then i thought the
std.heap.page_allocatormust have initialized the block of memory with170before giving it toxi.e.[]u8 - then i wondered why
170specifically? - then i speculated that maybe the last process which used this memory block must have initialized it to
170and os didn’t zero it out before giving it to this process. - but when i change the size of the block from
10to256and then512even then the entire block was initialized to170, it’s possible that the entire512bytes of memory was initialized to 170 but seems very unlikely to me. as i am writing this i realized that to test this hypothesis, i can reboot my pc and see if it still persists so i will do that now and i will post a comment under this post about it
at this point i had reached the end of my knowledge and couldn’t understand what was happening so here i am
please explain what’s happening here
thank you