I’m trying to make a FIFO array (which I called a Queue, but not sure what the conventional name for it is). allocating memory for the items field, always causes OutOfMemory error.
src/main.zig:17:26: error: expected type 'main.Queue(u32)', found 'error{OutOfMemory}'
.items = try allocator.alloc(T, capacity),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.zig:6:12: note: struct declared here
return struct {
^~~~~~
src/main.zig:14:56: note: function cannot return an error
fn init(allocator: Allocator, capacity: usize) Self {
Hey @weaea, can you rephrase your question a bit? There are two ways I can interpret what you are asking:
Why does allocator only returnOutOfMemory as an error?
Why do I need to put the ! in the return type?
Essentially, as @chung-leong pointed out, you have to have the ! in the return type because when something can return an error, you either need to use catch in your code to prevent it from passing through the function back to the caller or add a !Self or MyErrorType!Self to allow for an error union to be returned from your function.
To clarify, OutOfMemory is the possible error that alloc can return. Its not saying that the Allocator is out of memory, just that it is possible for that to be the case.
It’s not causing an OutOfMemory error, it is saying that is a possible outcome.
As @AndrewCodeDev pointed out, you will need to eiter update the init function to also say that it can return an error like !Self, or do a catch on the alloc call.
I’m sorry, I didn’t think I worded it badly.
What I’m trying to do is to allocate memory to a field in the struct, but it always fails with OutOfMemory error, so my question is how do correctly I allocate memory to a field in a struct?
The reason I’m confused about this behavior is that I’m essentially doing the same thing as in the code below, except that the code below works.
I didn’t see this reply before replying to @AndrewCodeDev. this solved my problem.
What happened is that I thought since I’m using the try keyword when allocating, that means the allocation doesn’t return the error anymore.
Turns out my understanding of the try keyword was incorrect.