pub fn addListStr(self: std.ArrayList([]const u8), text : []const u8) ! std.ArrayList([]const u8){
const allocator = std.heap.page_allocator;
var LIST = std.ArrayList([] const u8).init(allocator);
var iter = iteratStr.iterator(text);
for (self.items) | val | {
LIST.append(val) catch return ErrUtils.fld_ioField_addListStr_invalide;
}
while (iter.next()) |ch| {
LIST.append(ch) catch return ErrUtils.fld_ioField_addListStr_invalide;
}
self.deinit();
return LIST;
}
a) do variables die when exiting the function.
b) in this case how to make a defer !!! because result returns a value
does defer if i put it… will it release after transmitting the value???
I ask this question because my program works but it swells visibly
I also use on arraylist
but that did not transcend
x_FIELD = std.ArrayList([] const u8).init(allocator);
ex: defer x_FIELD.clearAndFree();
please i need clarification
c) I specify, I have no global variable apart from my declaration
const allocator = std.heap.page_allocator;
maybe you shouldn’t
It’s not clear what you’re trying to do, and you seem to be mixing []const u8 and u8 (one of those append calls looks like it should be a compile error).
Maybe it would be helpful to look at an example of a function that creates an ArrayList and returns a slice:
I finally understood the allocator system is what blocked and swelled the memory
especially with return std.fmt.allocPrint(allocator,“{s}”,.{result},) catch unreachable;
}
or
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
var allocator = arena.allocator();
pub fn deinitUtils() void {
arena.deinit();
arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
allocator = arena.allocator();
}
or
allocator.free(..) etc.
the problem is the use and in what condition it should be used.
but this is a very important part to understand when using zig-lang
we see examples but which do not correspond to real situations.
For my own edification here, help me understand what you’ve concluded. When you say “swells” do you mean that the memory was not getting deallocated?
It seemed odd that the original example was initializing the allocator in the same function (as opposed to taking the allocator in as an argument or, as in your last example, has it as a global variable). I can see how changing that would give you a better-behaved program.
No it’s not strange. Imagined several display modules, which has its own allocators, and also a lib with functions which also has its own allocators.
at each module exit I clean and keep the data to archive them, in Json and source code. (that’s what I’ve already done with nim-langue)
but I am preparing the principle.
despite daily work since Oct. 2022. I am still far from having understood all the subtleties.
I need to test and feel things, in order to reason ZIG-LANG.
For example:
this serving of bufPrint does not guarantee that the value is correctly assigned
while allocprint does it correctly
there I spent at least 4 days doing tests …
why I don’t guarantee the value, because it uses the same address on several bufprintz that follow. the last sets all the others to the same value.
so that justifies allocprint . …!!!