#Arrays allocation

1 messages · Page 1 of 1 (latest)

uncut otter
#

Hey guys, it's just a little question, if I get it right in Zig, I should also use ArrayList when I want to modify an array (remove, append) to it

#

Cause, I'd like to add to a field in a structure which I only know the value when code is being executed

#

so in code it's like

#
const MyStruct = struct {
  body: ?[50][]const u8;
}

const Body = MyStruct { .body = null }

Body = "something in there";
#

in my understanding of what I read on the internet it is supposed to make body mutable right

#

but is the whole object mutable ?

cedar gulch
#

body is an inline array. It's stored in the same memory as Body. It therefore has the same mutability.

uncut otter
#

OH

#

SO THATS THE CONST

#

THAT MAKES IT ???

#

OWO

fair talon
#

[50][]const u8 makes a mutable array of 50 slices, each pointing to an immutable array of u8

#

So you get an array of slices, but no array of u8

uncut otter
#

hmhhmh, I'll need more time to understand it

fair talon
#

[a number]a_type is an array of a_number a_type values
[_]a_type is an array of a_type values with an inferred size
[*]a_type is a many-item pointer to an a_type value
[]a_type is a slice of an array of a_type values

uncut otter
#

Ok so basically

#

Yes, ok that's the thing I don't think I can use an array as I don't know the size before hand

#

unless they are initialized to zero ?

fair talon
#

An array is basically a type, so it needs to have a size known at comptime

uncut otter
#

Oh yes makes a lot of sense, that's why I am using arrayList since I don't know the size

fair talon
#

If the size isn't known at comptime, then you have to use an allocator to get the memory needed to represent the data, and you use slices to access it

uncut otter
#

Oh alright alright

fair talon
#

ArrayList does it for you

uncut otter
#

But can we just allow like const char[50] string = {0};

#

so it initializes all bytes to zero

fair talon
#

You want an array of 50 strings set to zero?

uncut otter
#

I mean, just using whatever but basically, I was asking how mutability was working

#

because at first, I used to do const myVar = struct { }.. blabla and have ArrayList in it

#

but then it would tell me "cannot assign to a const"

#

which I wasn't understanding because for me the ArrayList inside the struct weren't const

#

but since you said they were in the same memory block

fair talon
#

All the elements of a struct are const if the struct is const

uncut otter
#

then it makes sense that now I have to use "var" instead of const

#

yup that makes everything have lot more sense

#

also, quick question is it bad to re-index a for loop ?

#

like I go from 1 to 10 and then at 10 I say go from 10 to a 100

#

but in the same loop

fair talon
#

Now if you want the struct to be const, but the array list object to be mutable, then you need the array list to be in a var, and the struct field to be a pointer to it

#

It's not bad! Maybe not the best way to do it depending on what's your goal, but it's not gonna hurt anything

uncut otter
#

would you mind showing code, sorry it's easier for me to understand

uncut otter
fair talon
#
const AStruct = struct {
    an_integer_pointer: *i8
};
var an_integer: i8 = 0;
const a_struct_instance = AStruct{
    .an_integer_pointer = &an_integer
};
a_struct_instance.an_integer_pointer.* = 1;```
uncut otter
#

Oh alright yes that makes sesnse now

uncut otter
#

so it my case

const Body = struct {
  body: *std.ArrayList(u8)
}

var bodyList: std.ArrayList(u8) = std.ArrayList(u8).init(allocator);

const BodyInstance = struct {
  .body: &bodyList
};

bodyList.body.append()..
#

is there any resource where I can read about the * operator you used

cedar gulch
uncut otter
#

Oh ok

cedar gulch
#

* before the type means it's a pointer

uncut otter
#

Ok ok makes sense

#

did a little bit of C so ok I get it

#

we deference to get to the value

#

and not the pointer

cedar gulch
#

it's also for writing to that memory

uncut otter
#

ok thank you so much ! I'm learning zig and it's been really nice

#

I'm gonna have to learn about the different types of allocators