#Arrays allocation
1 messages · Page 1 of 1 (latest)
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 ?
body is an inline array. It's stored in the same memory as Body. It therefore has the same mutability.
[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
hmhhmh, I'll need more time to understand it
[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
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 ?
An array is basically a type, so it needs to have a size known at comptime
Oh yes makes a lot of sense, that's why I am using arrayList since I don't know the size
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
Oh alright alright
ArrayList does it for you
But can we just allow like const char[50] string = {0};
so it initializes all bytes to zero
You want an array of 50 strings set to zero?
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
All the elements of a struct are const if the struct is const
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
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
would you mind showing code, sorry it's easier for me to understand
Alright then I'll go with it for my first iteration of my whatever I am doing
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;```
Oh alright yes that makes sesnse now
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
.* is pointer dereference
Oh ok
* before the type means it's a pointer
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
it's also for writing to that memory