December 24, 2025, 1:39am 1

Hello Everyone,

newbie here coming to zig from a high level language like python and I am trying to concatenate strings at the runtime using an allocator and have the below function, however the call is running into error{OutOfMemory}![]u8.

const std = @import("std");

fn cocat_strings(str1: []const u8, str2: []const u8) u8 {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer _ = gpa.deinit();
const combined = std.mem.concat(allocator, u8, &.{ str1, str2 });
defer allocator.free(combined);
return combined;
}

the caller looks like below

pub fn main() void {

const name: *const [3:0]u8 = "Som";
cocat_strings(name, name);  // looking for a result like "SomSom"
}

Any help with some explanation w…

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help